Php解析土豆flv
来源:互联网 发布:淘宝会员店加盟 编辑:程序博客网 时间:2024/06/11 03:16
(需要开启 php_curl.dll功能 ----- 用于支持SSL和zlib.)
示例URL $url=” http://hd.tudou.com/program/9473/”;
1、调用td_id($url)
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;
}
2、取得flv地址
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;
}
//
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;
}
//修改referer的值
(HTTP Referer是header的一部分,当浏览器向web服务器发送请求的时候,一般会带上Referer,告诉服务器我是从哪个页面链接过来的,服务器 籍此可以获得一些信息用于处理。比如从我主页上链接到一个朋友那里,他的服务器就能够从HTTP Referer中统计出每天有多少用户点击我主页上的链接访问他的网站)
Curl参考如下:http://www.cnblogs.com/phpliu/archive/2010/04/16/1713610.html
http://www.enjoyphp.com/2010/lamp/php-lamp/php-curl/
function curl_get_contents($url)
{
if(!function_exists('curl_init')) {
return false;
}else{
$user_agent = $_SERVER['HTTP_USER_AGENT']; //浏览器的类型
$ch = curl_init(); //启动一个curl会话
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); //模拟用户使用的浏览器,在HTTP请求中包含一个”user-agent”头的字符串
$content = curl_exec($ch); //执行操作
$content = curl_exec($ch);
curl_close($ch); //关闭CURL会话
if($errormsg != '') {
echo $errormsg;
return false;
}
&nbs