【发布时间】:2012-02-17 03:06:10
【问题描述】:
我正在使用以下代码下载 youtube 视频。
<?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>
我的 ge_youtube 函数包含在 youtube.lib.php 文件中。该文件包含代码..
<?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);
}
?>
输出对我来说很有趣。输出中没有显示错误。但我仍然一无所获。在代码中
> <a href="<?php echo trim($download_link);?>&fmt=17" class="ydl"
> title="Download as 3GP">Download 3GP</a>
显示在结果中,但链接指向 localhost。
看来我错过了一些技巧。让我告诉你我在尝试学习 php 和 curl 时发现了这个脚本。 你的任何建议或帮助?您能帮我把这段代码转换成工作代码吗?
谢谢
【问题讨论】:
标签: php regex curl download youtube