YouTube ActionScript 2.0 API
25 Nov 2006I had small requirement of YouTube API in one of my personal ActionScript 2.0 project, I couldn't find any ActionScript 2.0 version of YouTube API. I wrote it couple of hours.
You can look at source code and example (app I made to test it, quick and dirty) and you can download it with example files.
You would notice, to construct FLV URL, I am doing client side parsing of YouTube html page to find out "t" param. This doesn't work in Internet Explorer 6.0 (IE 6) because of a bug where IE 6.0 doesn't uncompress gzipped page if requested through LoadVars?
In production mode, I would prefer server-side proxy to find out "t" param because it's optimized:
- Would work in IE 6.0 and other browsers.
- Doesn't require loading YouTube html page ( > 20 KB) and parsing.
Update: I am using following PHP code to get FLV from YouTube asset servers.
<?php | |
// Youtube FLV Fetcher | |
// Responds to both HTTP GET and POST requests | |
// Author: Abdul Qabiz (abdul dot qabiz at gmail dot com) | |
// | |
// Description:Gets the path of FLV from YouTube URL | |
// Is it a POST or a GET? | |
if (isset($_GET['url'])) | |
{ | |
$url = $_GET['url']; | |
$v = $url; | |
$v = preg_split ("/\?v=/", $v); | |
//youtube video-id | |
$v = $v[1]; | |
//hardcoding URL here, couldn't figure how to make CURL follow 303 redirection | |
//seems something wrong with libCurl or my knowledge... | |
$url = "http://www.youtube.com/watch?v=" . $v; | |
//Start the Curl session | |
$session = curl_init(); | |
curl_setopt ($session, CURLOPT_URL, $url); | |
//you might want to turn it to false, in case you want | |
//to content to client (flash etc). | |
curl_setopt($session, CURLOPT_HEADER, true); | |
//not working for me :( | |
curl_setopt($session, CURLOPT_FOLLOWLOCATION, true); | |
//curl_setopt($ch, CURLOPT_TIMEOUT, 10); | |
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); | |
// Make the call | |
$response = curl_exec($session); | |
if (preg_match_all("/&t=[^&]*/", $response, $matches)) | |
{ | |
$t = $matches[0][0]; | |
$t = preg_split("/=/", $t); | |
//youtube t param | |
$t = $t[1]; | |
//construct the flv-url | |
$youtubeVideoPath = "http://www.youtube.com/get_video.php?video_id=" . $v . "&t=". $t . "&.flv"; | |
//redirect to flv - you can replace this code with echo/print | |
//to return the path to client (browser/flash) | |
header ("Location: $youtubeVideoPath"); | |
} else { | |
echo "null"; | |
} | |
curl_close($session); | |
} else { | |
echo "No YouTube URL to process"; | |
} | |
?> |
Technorati tags: youtube, flv, fetcher, download, php, curl, script