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. 没有图片

    2011.01.10

    自定义WordPress登录页面Logo

    通常自己设计WordPress主题,很少把后…

  2. 没有图片

    2010.06.04

    Fresh HTML V3.48英文绿色免费版

    Fresh HTML是一个支持所见即所得功能…

  3. 没有图片

    2010.05.19

    Comsenz 核心产品 Discuz! X1正式版发布

    Discuz! X1 Beta出来不久,今天…

  4. 没有图片

    2010.07.18

    最新手机软件推荐(一)

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

  5. 没有图片

    2010.10.26

    分享网页中插入Flv视频的代码

    今天给单位做网页,想用到流媒体格式,于是就想…

  6. 没有图片

    2010.07.23

    WordPress插件:向留言者发送文章更新通知邮件

    从ZWWoOoOo那里逛,发现一个强大的向留…

评论

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

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