PHP解析土豆FLV真实地址

刚与大家分享了PHP解析 KU6视频真实地址,本文再与大家分享一下PHP解析土豆FLV真实地址,方法如下:

要求:服务器必须支持CURL或fsockopen。

详细代码:

<?php
function curl_get_contents($url)
{
if(!function_exists(‘curl_init’)) {
return false;
} else {
$user_agent = $_SERVER[‘HTTP_USER_AGENT’];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
$content = curl_exec($ch);
$errormsg = curl_error($ch);
curl_close($ch);
if($errormsg != ”) {
echo $errormsg;
return false;
}
return $content;
}
}

function fsock_get_contents($url)
{
if(!function_exists(‘fsockopen’)) {
return false;
} else {
$user_agent = $_SERVER[‘HTTP_USER_AGENT’];
$url = eregi_replace(‘^http://’, ”, $url);
$temp = explode(‘/’, $url);
$host = array_shift($temp);
$path = ‘/’.implode(‘/’, $temp);
$temp = explode(‘:’, $host);
$host = $temp[0];
$port = isset($temp[1]) ? $temp[1] : 80;
$fp = @fsockopen($host, $port, $errno, $errstr, 30);
if ($fp){
@fputs($fp, “GET $path HTTP/1.1rnHost: $hostrnAccept: */*rnReferer:$urlrnUser-Agent: $user_agentrnConnection: Closernrn”);
}
$content = ”;
while ($str = @fread($fp, 4096)){
$content .= $str;
}
@fclose($fp);
//Redirect
if(preg_match(“/^HTTP/d.d 301 Moved Permanently/is”, $content)){
if(preg_match(“/Location:(.*?)rn/is”, $content, $murl)){
return fsock_get_contents($murl[1]);
}
}
//Read contents
if(preg_match(“/^HTTP/d.d 200 OK/is”, $content)){
preg_match(“/Content-Type:(.*?)rn/is”, $content, $murl);
$contentType = trim($murl[1]);
$content = explode(“rnrn”, $content, 2);
$content = $content[1];
}
return $content;
}
}

function get_page_contents($url)
{
$content = ”;
if(function_exists(‘curl_init’)) {
$content = curl_get_contents($url);
} elseif (function_exists(‘fsockopen’)) {
$content = curl_get_contents($url);
} else echo “函数 curl_init 和 fsockopen 都为关闭,请至少打开一个.”;
return $content;
}

function td_id($url)
{
if(preg_match(‘/http://hd.tudou.com/program/([A-Za-z0-9-_]+)/’, $url, $hd)) {
$target_url = “http://hd.tudou.com/program/”.$hd[1];
} elseif (preg_match(‘/http://www.tudou.com/programs/view/([A-Za-z0-9-_]+)/’, $url, $td)) {
$target_url = “http://www.tudou.com/programs/view/”.$td[1];
}

$content = get_page_contents($target_url);
preg_match(‘/iid.*?([0-9]+)/’, $content, $tudou);
$tudou_id = $tudou[1];

return $tudou_id;
}

function get_flv($video_id)
{
$flv_link = “”;

$target_url = ‘http://v2.tudou.com/v2/cdn?id=’.$video_id;

$video_data = get_page_contents($target_url);

preg_match(‘/<f w=”([0-9]+)”/’, $video_data, $match_num);
preg_match(‘/http://(.*?)?/’, $video_data, $match_url);
preg_match(‘/key=([w]+)/’, $video_data, $match_key);
$flv_link = ‘http://’.$match_url[1].’?’.$match_num[1].’&key=’.$match_key[1].’&id=tudou’;
return $flv_link;
}
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>PHP解析土豆FLV真实地址</title>
</head>

<body>
支持以下两种格式<br />
<a href=”javascript://” onclick=”document.getElementById(‘url’).value = this.title;” title=”http://hd.tudou.com/program/6358/?isRenhe=1″>http://hd.tudou.com/program/6358/?isRenhe=1</a><br />
<a href=”javascript://” onclick=”document.getElementById(‘url’).value = this.title;” title=”http://www.tudou.com/programs/view/CsaJSSBC6T8/”>http://www.tudou.com/programs/view/CsaJSSBC6T8/</a><br />
<form action=”<?php echo $_SERVER[‘PHP_SELF’]; ?>” method=”post”>
<label>URL
<input type=”text” name=”url” id=”url” style=”width:300px;” />
</label>
<label>
<input type=”submit” name=”button” id=”button” value=”解析” />
</label>
</form>
<div style=”clear:both;”></div>
<?php
if($_POST) {
echo ‘FLV真是地址:’.get_flv(td_id($_POST[‘url’]));
}
?>
</body>
</html>

可以把上面代码复制到dutou.php文件,直接使用。

点击直接在线使用:tudou.php

本文代码转自:http://cheon.info

思章老师

认准了方向,就要勇敢地走下去,十年磨一剑,我相信,只要坚持,一切都有可能。

相关日志

  1. 没有图片

    2010.07.18

    最新手机软件推荐(一)

    小百度今天开始给手机玩家推荐最新的手机软件,…

  2. 没有图片

    2010.05.12

    40个免费的WordPress简洁主题

    蓦然很负责任地说,好主题还是要用国外的,这可…

  3. 没有图片

    2011.03.20

    菠萝筐终于被驱逐出境!博客空间搬入美国!

    不知道为什么,菠萝的博客备案被无故注销了,于…

  4. 没有图片

    2010.05.18

    改变人生的32句励志名言(转载)

    1、大多数人想要改造这个世界,但却罕有人想改…

  5. 没有图片

    2011.06.30

    博客升级到WordPress 3.1.4 顺便向兄弟们道歉

    本来打算睡觉了,发现WP后台显示了升级到Wo…

  6. 没有图片

    2010.09.05

    笔记本U盘启动的设置方法

    本来这是个很简单的问题,发现有很多朋友问如何…

评论

  1. elsesky 2011.03.13 12:13下午

    其实我一直想知道为啥要用socket函数。。。直接file_get_contents不就完了。。。
    $content = file_get_contents($target_url); 替换掉$content = get_page_contents($target_url);

  2. tatters 2010.12.02 6:27下午

    你好,看了你的这两篇获取视频真实地址的文章,感觉找到了知音。但是点击了你网站的链接tudou.php和ku6.php,可以点开,但是输入地址后,却无法解析,转到了“No Results Found”页面,这是为什么呢?另外,可否再分享一个解析优酷youku的方法呢?不胜感谢!!

  3. nikedunksb 2010.09.27 1:05下午

    沙发
    看不懂

    • 小百度 2010.10.11 1:32下午

      可以得到真实视频地址的。