Quick piece of code that should, in theory, fetch the FLV/MP4/whatever URL of the video, given a youtube URL.
The example has a hard-coded url, turn it into a function to make it a parameter instead.
Also, it has absolutely no error checking, so you may wanna implement that (e.g. you should check if $r returns false and, if so, throw an error and die).
This results in an array containing arrays like so:
Just an example anyway, there may be an easier way to do it but this seemed logical at the time.
The example has a hard-coded url, turn it into a function to make it a parameter instead.
Also, it has absolutely no error checking, so you may wanna implement that (e.g. you should check if $r returns false and, if so, throw an error and die).
PHP:
$c = curl_init();
curl_setopt($c, CURLOPT_URL, 'http://www.youtube.com/watch?v=sRLz4aDCAs0');
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_FAILONERROR, 1);
$r = curl_exec($c);
curl_close($c);
preg_match_all('#url_encoded_fmt_stream_map=([^&]+)#', $r, $fvars);
$fvars = explode('&', urldecode($fvars[1][0]));
$results = array();
$resultIndex = -1;
foreach($fvars as $item)
{
if(strpos($item, 'url=') !== false)
$resultIndex++;
if(!isset($results[$resultIndex]))
$results[$resultIndex] = array();
if(strpos($item, 'url=') === false)
{
$parse = explode('=', $item);
$results[$resultIndex][$parse[0]] = urldecode($parse[1]);
continue;
}
$parse = preg_match('#url=(.+)$#', $item, $url);
$results[$resultIndex]['url'] = urldecode($url[1]);
}
var_dump($results);
This results in an array containing arrays like so:
Code:
array(5) {
["url"]=>
string(419) "some_youtube_url"
["quality"]=>
string(5) "small"
["fallback_host"]=>
string(26) "tc.v4.cache5.c.youtube.com"
["type"]=>
string(11) "video/x-flv"
["itag"]=>
string(1) "5"
}
Just an example anyway, there may be an easier way to do it but this seemed logical at the time.