Combining things I had in my original answer, plus some of the ideas in Brad's answer, I offer the following solution
<?php
$url='http://r16---sn-4g57kn6e.googlevideo.com/videoplayback?&quality=medium&signature=797C0FEB1961E6226294D5FC19BC0CD28657975C.1E745D852200D14B706F0EBF9EA8762680374564&itag=43&mv=m&ip=84.19.165.220&ipbits=0&ms=au&ratebypass=yes&source=youtube&mt=1390347607&id=8b92b07ff9cd9862&key=yt5&fexp=942502,916626,929305,936112,924616,936910,936913,907231,921090&upn=cMPazwtmyZU&sver=3&sparams=id,ip,ipbits,itag,ratebypass,source,upn,expire&expire=1390371882&type=video%2Fwebm%3B+codecs%3D%22vp8.0%2C+vorbis%22&fallback_host=tc.v12.cache5.googlevideo.com&title=Requiem+For+A+Dream+Original+Song&title=Requiem For A Dream Original Song';
$cleanUrl = parseQuery($url);
$data = getData($cleanUrl);
echo "file read in OK\n";
function parseQuery($url) {
preg_match('/(https?:\/\/[^?]+\?)(.*)$/', $url, $rawQuery);
preg_match_all('/([^=]+)=([^&]+)&/', $rawQuery[2], $queries);
$qArray = array_combine($queries[1], $queries[2]);
$newUrl = $rawQuery[1] . http_build_query($qArray);
return $newUrl;
}
function getData($url) {
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
?>
This basically involves the following steps:
- Take the initial URL, and split it into "stuff before the
?
, and stuff after"
- The "stuff before" is untouched; the "stuff after" is split into two array - the keys and the values of the query ("everything up to
=
", and "everything up to &
")
- These two arrays are then combined into a valid query string using (from Brad's answer) the
http_build_query
array
- Finally, I use
curl
to fetch the file (just because I know it better than readfile()
).
It appears to work for me. Let me know if it doesn't work for you...