Categories
PHP Work

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

THE API HAS BEEN BROKEN FOR A LONG TIME, AND I DON’T WANT TO FIX IT SINCE THERE ARE BETTER ALTERNATIVES LIKE “youtube-dl”.

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: https://github.com/masnun/phptube 🙂

An example:

Output:

41 replies on “PhpTube: A PHP Class to get download links from Youtube Watch URLs”

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.

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.

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

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.

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 ?

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 🙂

An error ocurred: Error Locating Download URL’s I think smth were changed on youtube.com((((

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

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 🙂

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

Hello,
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

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.

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 = ‘http://youtube.com/get_video_info?video_id=’.$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?

 

your code is bugs #Output Notice: Undefined offset: 0 in E:\xampp\htdocs\apitest\PhpTube.php on line 23array(0) { }

Comments are closed.