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.05.20

    月光论坛事件与国王的故事

    曾经有一个人,骂国王是笨蛋,后来他被抓起来交…

  2. 2013.01.01

    2013年第一款 WordPress CMS主题: XCMS V1.0发布

    继2012年最后一款主题: X2013 发布…

  3. 没有图片

    2010.06.05

    手机浏览器 UC浏览器7.1正式版 For iPhone

    随着3G网络的流行,手机上网已经成为大家很正…

  4. 没有图片

    2010.06.18

    WordPress 3.0 繁体中文正式版发布

    就在小百度刚刚发布WordPress 3.0…

  5. 没有图片

    2015.06.09

    关于中搜互联网业务的评价(转)

    这几天,因一位年近60的老朋友商大叔接到来自…

  6. 没有图片

    2010.09.12

    老师,您还记得什么是育人吗?

    前天是教师节,和平常一样,平平淡淡地过,当然…

评论

  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下午

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