PhpTube: A PHP Class to get download links from Youtube Watch URLs


Let me get to it straight: I wrote a php class that takes a youtube watch url (the typical url to watch a video, the url has a “watch” GET parameter in it) and returns the download links for different available formats of the video. Please note that these links will be valid for the requesting IP address. That is you can not download the videos from other IP addresses except the only one IP adress that actually originated the download request. So if you put it on your server and send these links to your users, they’ll certainly not be able to download from these links since their IP addresses will be different from your server IP address. In that case, you should first download it to your server and then link to those downloaded videos.

You can download the source codes from my github repo: 🙂

An example:


40 Comments PhpTube: A PHP Class to get download links from Youtube Watch URLs

  1. maSnun

    Yes, the download URLs have an “expiry” part. And also, these links will not work for anyone else. These were generated from my IP address.

  2. Salman Morshed

    I think if you add the video to a playlist on your youtube account, the video remains cached in youtube servers for your account. So if you can generate the links from the IP you logged in, it may not expire. I’m not sure, but I think I read something like this before.

  3. Sarim Khan

    Does it work now ? I’m getting string(46) “An error ocurred: Error Locating Downlod URL’s”

    1. maSnun

      For some reasons the video doesn’t have any other formats specified. So it has no download candidates. I have added a meaningful error message in such cases. Thanks for the input.

  4. Sarim Khan

    Is there any need to use the curl ?

    private function _getHtml($url)
    if (function_exists("curl_init")) {

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    return curl_exec($ch);
    } else {
    // throw new Exception("No cURL module available");
    return file_get_contents($url);


    How’s that ?

    1. maSnun

      cURL is more acceptable to me than file_get_contents() because many hosts have fopen() disabled for urls. In such cases, file_get_contents() shall not work. But I like your solution, but put that file_get_contents() in another if…else loop to to check for data, if it fails, throw an exception.

      If you make a pull request on github, I shall pull it in 🙂

  5. ronen4kill

    when i download the video and changing the name to a.flv and tries to open it with media player or vlc its says occurred file

    1. maSnun

      The videos on that channel might have something different that is breaking the entire thing. I am sorry, I can’t really afford to look into the issue right now. May be when I get some time. I of course welcome anyone interested in sniffing the youtube html and fixing the issue. Proper credits will be given 🙂

          1. Robert Lerner

            You could potentially pass the session ID from the browser through cURL. Once logged in, the age restriction should go away.

  6. Pingback: Generate actual Youtube download link in PHP or Javascript | - Developers Network

  7. Eugene

    How can i use the links?? it’s telling me 403 forbidden even though i’m using the same ip.

    How can i fix this please

  8. Analisa Emas

    Woah! I’m really loving the template/theme of this site. It’s simple,
    yet effective. A lot of times it’s very difficult to get that “perfect balance” between superb usability and appearance. I must say that you’ve done a great job with this.
    In addition, the blog loads very fast for me on Chrome.

    Excellent Blog!


    Great post. I was checking constantly this blog and I’m inspired! Very helpful information particularly the remaining phase 🙂 I take care of such information a lot. I used to be seeking this particular information for a long time. Thank you and best of luck.

  10. Ali

    I tried to ran the sample script but it didn’t work. I tried to print the array to analyze the output. However, the array is empty.

    Array ( [0] => Array ( ) [1] => Array ( ) )

    I also tried to write my own script too. Below is a sample code:


    $url = ‘’.$id;

    $content = file_get_contents($url);

    Then I parse the contents to get the direct link. However, for some videos (such as video ID: a1Nmb7OEpQA) this method also does not work. I mean, it produces the link but that link not downloadable or playable. Do you know what could be the reason?



Leave a Reply

Your email address will not be published. Required fields are marked *