Skip to content

Instantly share code, notes, and snippets.

@2no
Created July 18, 2011 11:55
Show Gist options
  • Save 2no/1089296 to your computer and use it in GitHub Desktop.
Save 2no/1089296 to your computer and use it in GitHub Desktop.
Flash などから Youtube の動画を読み込む為のプロキシ。後は crossdomain.xml を設置
<?php
// usage:
// youtube_proxy.php?url=http%3a%2f%2fwww%2eyoutube%2ecom%2fwatch%3fv%3dmxPXPv3oNY4%26feature%3dfvsr
define('YOUTUBE_DOMAIN', 'www.youtube.com');
// fmt(優先度:左->右)
$fmtTbl = array(5, 18, 34, 22, 37);
$url = isset($_GET['url']) ? rawurldecode($_GET['url']) : '';
$pattern = '/^http:\/\/' . preg_quote(YOUTUBE_DOMAIN) . '\//';
if (preg_match($pattern, $url)) {
$html = @file_get_contents($url);
if (preg_match('/"fmt_url_map": "([^"]*)",/i', $html, $matches)) {
$urls = explode(',', $matches[1]);
$flv = array();
foreach ($urls as $url) {
if (preg_match('/^(\d+)\|(.*?)$/', $url, $matches)) {
$flvUrl = urldecode($matches[2]);
$flvUrl = str_replace('\u0026', '&', $flvUrl);
$flvUrl = str_replace('\\', '', $flvUrl);
$flv[(int)$matches[1]] = $flvUrl;
}
}
if ($flv) {
foreach ($fmtTbl as $fmt) {
if (isset($flv[$fmt])) {
header('Content-type: video/x-flv');
readfile($flv[$fmt]);
exit;
}
}
}
}
}
header('HTTP/1.0 404 Not Found');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment