服务器必须支持CURL或fsockopen
这个解析方法将在PHP Melody 1.5.4中文版中使用到


经过一晚上的测试,测试,再测试,发现个别解析到的地址用不了。严格的话就要循环检查一下,但是这样太费系统资源和时间了,程序运行起来会很慢,解决方法在研究中。。。。。。


演示地址:http://cheon.info/tudou.php



 







001 <?php






002 function curl_get_contents($url)






003 {






004     if(!function_exists(‘curl_init’)) {






005         return false;






006     } else {






007         $user_agent = $_SERVER[‘HTTP_USER_AGENT’];






008         $ch = curl_init();






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);






015         curl_close($ch);






016         if($errormsg != ) {






017             echo $errormsg;






018             return false;






019         }






020     return $content;






021     }






022 }






023   






024 function fsock_get_contents($url)






025 {






026     if(!function_exists(‘fsockopen’)) {






027         return false;






028     } else {






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);






035         $host = $temp[0];






036         $port = isset($temp[1]) ? $temp[1] : 80;






037         $fp = @fsockopen($host, $port, $errno, $errstr, 30);






038         if ($fp){






039             @fputs($fp, “GET $path HTTP/1.1rnHost: $hostrnAccept: */*rnReferer:$urlrnUser-Agent: $user_agentrnConnection: Closernrn”);






040         }






041         $content = ;






042         while ($str = @fread($fp, 4096)){






043             $content .= $str;






044         }






045         @fclose($fp);






046         //Redirect






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]);






050             }






051         }






052         //Read contents






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];






058         }






059         return $content;






060     }






061 }






062   






063 function get_page_contents($url)






064 {






065     $content = ;






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 都为关闭,请至少打开一个.”;






071     return $content;






072 }






073   






074 function td_id($url)






075 {






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];






080     }






081   






082     $content = get_page_contents($target_url);






083     preg_match(‘/iid.*?([0-9]+)/’, $content, $tudou);






084     $tudou_id = $tudou[1];






085   






086     return $tudou_id;






087 }






088   






089 function get_flv($video_id)






090 {






091     $flv_link = “”;






092   






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






094   






095     $video_data = get_page_contents($target_url);






096   






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’;






101     return $flv_link;






102 }






103 ?>






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”>






106 <head>






107 <meta http-equiv=“Content-Type” content=“text/html; charset=utf-8” />






108 <title>PHP解析土豆FLV真实地址</title>






109 </head>






110   






111 <body>






112 支持以下两种格式<br />






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”>






116     <label>URL






117         <input type=“text” name=“url” id=“url” style=“width:300px;” />






118     </label>






119     <label>






120         <input type=“submit” name=“button” id=“button” value=“解析” />






121     </label>






122 </form>






123 <div style=“clear:both;”></div>






124 <?php






125 if($_POST) {






126 echo ‘FLV真是地址:’.get_flv(td_id($_POST[‘url’]));






127 }






128 ?>






129 </body>






130 </html>

Comments are closed.

Post Navigation