服务器必须支持CURL或fsockopen
这个解析方法将在PHP Melody 1.5.4中文版中使用到
经过一晚上的测试,测试,再测试,发现个别解析到的地址用不了。严格的话就要循环检查一下,但是这样太费系统资源和时间了,程序运行起来会很慢,解决方法在研究中。。。。。。
演示地址:http://cheon.info/tudou.php
002 |
function curl_get_contents( $url ) |
004 |
if (!function_exists( ‘curl_init’ )) { |
007 |
$user_agent = $_SERVER [ ‘HTTP_USER_AGENT’ ]; |
009 |
curl_setopt( $ch , CURLOPT_URL, $url ); |
010 |
curl_setopt( $ch , CURLOPT_HEADER, 0); |
011 |
curl_setopt( $ch , CURLOPT_RETURNTRANSFER, 1); |
012 |
curl_setopt( $ch , CURLOPT_USERAGENT, $user_agent ); |
013 |
$content = curl_exec( $ch ); |
014 |
$errormsg = curl_error( $ch ); |
024 |
function fsock_get_contents( $url ) |
026 |
if (!function_exists( ‘fsockopen’ )) { |
029 |
$user_agent = $_SERVER [ ‘HTTP_USER_AGENT’ ]; |
030 |
$url = eregi_replace ( ‘^http://’ , ” , $url ); |
031 |
$temp = explode ( ‘/’ , $url ); |
032 |
$host = array_shift ( $temp ); |
033 |
$path = ‘/’ .implode( ‘/’ , $temp ); |
034 |
$temp = explode ( ‘:’ , $host ); |
036 |
$port = isset( $temp [1]) ? $temp [1] : 80; |
037 |
$fp = @ fsockopen ( $host , $port , $errno , $errstr , 30); |
039 |
@ fputs ( $fp , “GET $path HTTP/1.1rnHost: $hostrnAccept: */*rnReferer:$urlrnUser-Agent: $user_agentrnConnection: Closernrn” ); |
042 |
while ( $str = @ fread ( $fp , 4096)){ |
047 |
if (preg_match( “/^HTTP/d.d 301 Moved Permanently/is” , $content )){ |
048 |
if (preg_match( “/Location:(.*?)rn/is” , $content , $murl )){ |
049 |
return fsock_get_contents( $murl [1]); |
053 |
if (preg_match( “/^HTTP/d.d 200 OK/is” , $content )){ |
054 |
preg_match( “/Content-Type:(.*?)rn/is” , $content , $murl ); |
055 |
$contentType = trim( $murl [1]); |
056 |
$content = explode ( “rnrn” , $content , 2); |
057 |
$content = $content [1]; |
063 |
function get_page_contents( $url ) |
066 |
if (function_exists( ‘curl_init’ )) { |
067 |
$content = curl_get_contents( $url ); |
068 |
} elseif (function_exists( ‘fsockopen’ )) { |
069 |
$content = curl_get_contents( $url ); |
070 |
} else echo “函数 curl_init 和 fsockopen 都为关闭,请至少打开一个.” ; |
076 |
if (preg_match( ‘/http://hd.tudou.com/program/([A-Za-z0-9-_]+)/’ , $url , $hd )) { |
077 |
$target_url = “http://hd.tudou.com/program/” . $hd [1]; |
078 |
} elseif (preg_match( ‘/http://www.tudou.com/programs/view/([A-Za-z0-9-_]+)/’ , $url , $td )) { |
079 |
$target_url = “http://www.tudou.com/programs/view/” . $td [1]; |
082 |
$content = get_page_contents( $target_url ); |
083 |
preg_match( ‘/iid.*?([0-9]+)/’ , $content , $tudou ); |
084 |
$tudou_id = $tudou [1]; |
089 |
function get_flv( $video_id ) |
093 |
$target_url = ‘http://v2.tudou.com/v2/cdn?id=’ . $video_id ; |
095 |
$video_data = get_page_contents( $target_url ); |
097 |
preg_match( ‘/<f w=”([0-9]+)”/’ , $video_data , $match_num ); |
098 |
preg_match( ‘/http://(.*?)?/’ , $video_data , $match_url ); |
099 |
preg_match( ‘/key=([w]+)/’ , $video_data , $match_key ); |
100 |
$flv_link = ‘http://’ . $match_url [1]. ‘?’ . $match_num [1]. ‘&key=’ . $match_key [1]. ‘&id=tudou’ ; |
104 |
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd” > |
105 |
<html xmlns= “http://www.w3.org/1999/xhtml” > |
107 |
<meta http-equiv= “Content-Type” content= “text/html; charset=utf-8” /> |
108 |
<title>PHP解析土豆FLV真实地址</title> |
113 |
<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 /> |
114 |
<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 /> |
115 |
<form action= “<?php echo $_SERVER[‘PHP_SELF’]; ?>” method= “post” > |
117 |
<input type= “text” name= “url” id= “url” style= “width:300px;” /> |
120 |
<input type= “submit” name= “button” id= “button” value= “解析” /> |
123 |
<div style= “clear:both;” ></div> |
126 |
echo ‘FLV真是地址:’ .get_flv(td_id( $_POST [ ‘url’ ])); |