PDA

View Full Version : YouTube PHP assistance



Dean
05-11-2010, 08:51 PM
I've been attempting to get hold of a script for about 3 days now that will basically get the video ID and the token from a YouTube video, then echo it. Unfortunately, I've failed.

This is the coding that I'm using...


<?php
$youtube = $_GET['web'];
function getstring($string)
{
$pos = strrpos($string, "swfArgs")+11;
$texto = substr($string,$pos,strrpos(substr($string,$pos,st rlen($string)), "additionalStatsHonorsUrl")-10);
$replace = array(" ",'"',"_");
$adata = split(",",str_replace($replace,"",$texto));
$vid_id = split(":",$adata[2]);
$code = split(":",$adata[6]);
$vidn1 = strrpos($string, "<title>")+7;
$vidn2 = strrpos(substr($string,$vidn1,-1), "</title>");
$vidname = substr($string,$vidn1,$vidn2);
$web = 'http://www.youtube.com/get_video?fmt=18&video_id='.$vid_id[1].'&t='.$code[1];
echo $web.'|'.$vidname;
}
function getPage($opts) {
$ch = curl_init($opts);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.12) Gecko/20070508 Firefox/1.5.0.12");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$html = curl_exec($ch);
if(curl_errno($ch)) {
$html = "";
}
curl_close ($ch);
return getstring($html);
}
getPage($youtube);
?>

For example... this YouTube link...


http://www.youtube.com/watch?v=m2vWQFHP3D0

I should be able to get the video ID from the URL already which is m2vWQFHP3D0 but then I need the token.

The end product should look like this...


http://www.youtube.com/get_video?fmt=18&video_id=m2vWQFHP3D0&t=vjVQa1PpcFNL59klwGHWg7fOm6Haf1Ytrvgl9NPE32Q=&asv=

BUT... I get this :S I really don't understand!


http://www.youtube.com/get_video?fmt=18&video_id=//s.ytimg.com/yt/cssbin/www-feather-vfl54jP1d.css><!--[iflteIE7]><linkrel=stylesheethref=http&t=//s.ytimg.com/yt/img/pixel-vfl3z5WfW.gifclass=opera-link></a><ahref=http

If anyone knows what's up, then please feel free to help. +rep for anyone that contributes well.

Dentafrice
06-11-2010, 03:13 PM
Here's something I threw together. You can use regex but for the sake of understanding on your end I used explode.

You can input it into generate_url two different ways and it will still work. You can either input a full URL like $url does, or just give it the movie ID (m2vWQ..etc)



$url = "http://www.youtube.com/watch?v=m2vWQFHP3D0";

function get_movie($url)
{
$video = explode('v=', $url);
$video = $video[1];
$video = explode('&', $video);
$video = $video[0];

return $video;
}

function get_token($url)
{
$html = file_get_contents($url);

$token = explode('"t": "', $html);
$token = explode('",', $token[1]);
$token = $token[0];

return $token;
}

function construct_url($video)
{
if (explode('youtube.com', $video) != -1) {
$token = get_token($video);
$video = get_movie($video);
} else {
$token = get_token("http://www.youtube.com/watch?v={$video}");
}

return "http://www.youtube.com/get_video?fmt=18&video_id={$video}&t={$token}&asv=";
}


echo construct_url($url);

Dean
06-11-2010, 04:00 PM
Here's something I threw together. You can use regex but for the sake of understanding on your end I used explode.

You can input it into generate_url two different ways and it will still work. You can either input a full URL like $url does, or just give it the movie ID (m2vWQ..etc)



$url = "http://www.youtube.com/watch?v=m2vWQFHP3D0";

function get_movie($url)
{
$video = explode('v=', $url);
$video = $video[1];
$video = explode('&', $video);
$video = $video[0];

return $video;
}

function get_token($url)
{
$html = file_get_contents($url);

$token = explode('"t": "', $html);
$token = explode('",', $token[1]);
$token = $token[0];

return $token;
}

function construct_url($video)
{
if (explode('youtube.com', $video) != -1) {
$token = get_token($video);
$video = get_movie($video);
} else {
$token = get_token("http://www.youtube.com/watch?v={$video}");
}

return "http://www.youtube.com/get_video?fmt=18&video_id={$video}&t={$token}&asv=";
}


echo construct_url($url);


Thank you so much! +rep! (I have to spread, so I'll do it ASAP)

Only, I have one problem that I've run into. It seems that I can only use the link if I manually do it, because of the I.P. address...


The part about the "real" URL, the one containing the requester's IP address, is still true. It's based on the requester's IP address and changing the IP address argument will break it. However, try to use the normal mp4 download URL that you get on one computer on a computer on a different network and it will fail with a 403 error.

ie.
http://www.youtube.com/get_video?fmt=18&video_id=zR22IL6WhxE&t=vjVQa1PpcFNUPqgbZqTou4ZdBV-VGIPHKitNuCdfX8M=


will work on the computer that you source it from, but won't work on a computer on a different network :/

Dentafrice
06-11-2010, 04:22 PM
If I remember correctly that is the way the token works...

Dean
06-11-2010, 04:28 PM
Surely there's a way of bypassing this?

Joe!
06-11-2010, 10:58 PM
You could download it the server using curl and then download from your server? Obviously that uses quite a lot of resources.
OR rewrite it in javascript, so the client actually generates the url :)

Dean
07-11-2010, 03:20 AM
You could download it the server using curl and then download from your server? Obviously that uses quite a lot of resources.
OR rewrite it in javascript, so the client actually generates the url :)

I've tried curl and that didn't work, just outputted a bunch of crap like I said in my first post. How would I go about doing it in javascript?

Want to hide these adverts? Register an account for free!