问题
i am using following code for downloading youtube video.
<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
require_once('lib/youtube.lib.php');
if(preg_match('/youtube\.com/i',$_GET['url'])){
if(!preg_match('/www\./i',$_GET['url'])){
$_GET['url'] = str_replace('http://','http://www.',$_GET['url']);
}
list($video_id,$download_link) = get_youtube($_GET['url']);}
else{
die('<span style="color:red;">Sorry, the URL is not recognized..</span>');
}
?>
<p>
<img src="http://img.youtube.com/vi/<?php echo trim($video_id);?>/1.jpg" alt="Preview 1" class="ythumb" />
<img src="http://img.youtube.com/vi/<?php echo trim($video_id);?>/2.jpg" alt="Preview 2" class="ythumb" />
<img src="http://img.youtube.com/vi/<?php echo trim($video_id);?>/3.jpg" alt="Preview 3" class="ythumb" />
</p>
<p>
<a href="<?php echo trim($download_link);?>" class="ydl" title="Download as FLV">Download FLV</a>
<a href="<?php echo trim($download_link);?>&fmt=35" class="ydl" title="Download as MP4">Download MP4</a>
<a href="<?php echo trim($download_link);?>&fmt=17" class="ydl" title="Download as 3GP">Download 3GP</a>
</p>
and my ge_youtube function is included in youtube.lib.php file. the file contains code..
<?php
function get_content_of_url($url){
$ohyeah = curl_init();
curl_setopt($ohyeah, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ohyeah, CURLOPT_URL, $url);
$data = curl_exec($ohyeah);
curl_close($ohyeah);
//print_r($data);
return $data;
}
function get_flv_link($string) {
if (preg_match('/watch_fullscreen(.*)plid/i', $string, $out)){
if (!preg_match('/watch_fullscreen(.*)plid/i', $data, $out)) {
$outdata = $out[1];
echo '1'.'<br>';
$arrs = (explode('&',$outdata));
foreach($arrs as $arr){
list($i,$x) = explode("=",$arr);
$$i = $x;
}
$link = 'http://www.youtube.com/get_video?video_id='.$video_id.'&t='.$t;
echo '2';
echo $link;
array($video_id,$link);
return array($video_id,$link);
}
}
}
function get_youtube($url){
$stream = get_content_of_url($url);
return get_flv_link($stream);
}
?>
the output is very interesting for me. there is no error displayed in output. but still i can get notthing. in code
> <a href="<?php echo trim($download_link);?>&fmt=17" class="ydl"
> title="Download as 3GP">Download 3GP</a>
is displayed in result but the link points to localhost.
It seems that i am missing some trick. Let me tell you i found this script while trying to learn php and curl .. any suggesstion or help from you?? Would you please help me convert this code to a working code?
thanks
回答1:
The problem isn't the regex, the problem is the assumptions of YouTube's page format that the library makes. Replace:
}
function get_youtube($url){
with
else { echo "No match"; }
}
function get_youtube($url){
(which adds an else
to the if
in get_flv_link()
) and you'll get a message saying that the content doesn't match. This probably means that YouTube have altered their page format (possibly because people were trying to scrape it like this).
Also, I'm confused by:
if (preg_match('/watch_fullscreen(.*)plid/i', $string, $out)){
if (!preg_match('/watch_fullscreen(.*)plid/i', $data, $out)) {
That says "if it matches then check if it doesn't match on a different variable (which hasn't been declared at this point)". Even if the page content matched the expected content then you'd probably have other bits that needed fixing in the library.
来源:https://stackoverflow.com/questions/8977169/youtube-video-download-in-php-using-regex