-
-
/**
- * 取得youku视频的最终地址
- * edit by bbs.it-home.org
- * at: 2013-10-2
- */
- class youkuflv{
- static private $error = "";
- static private $result = array();
-
- static public function getyoukuflv($url){
- //从url获取youkuid
- if(! $id = self::getyoukuid($url)){
- return false;
- }
- //获取youku视频详细信息
- $content = self::get_curl_contents( "http://v.youku.com/player/getplaylist/videoids/".$id );
- $data = json_decode($content);
- if(!isset($data->data[0]->streamfileids)){
- self::$error = "cannot find this video";
- return false;
- }
- foreach($data->data[0]->streamfileids as $k=>$v){
- if($k == 'flv' || $k == 'mp4'){
- //sid
- $sid= self::getsid();
- //fileid
- $fileid = self::getfileid($v,$data->data[0]->seed);
- $one=($data->data[0]->segs->$k);
- self::$result[$k] = "http://f.youku.com/player/getflvpath/sid/{$sid}_00/st/{$k}/fileid/{$fileid}?k={$one[0]->k}";
- }
- }
- if(empty(self::$result)){
- self::$error = "this viod is not in mp4 or flv format";
- return false;
- }else{
- return true;
- }
- }
- static public function error(){
- return self::$error;
- }
-
- static public function result(){
- return self::$result;
- }
-
- static private function getyoukuid($url){
- //url 不能为空
- if($url == "" || substr($url , 0 , 29) != "http://v.youku.com/v_show/id_"){
- self::$error = "url is error";
- return false;
- }
- return substr($url , 29 , -5);
- }
-
- static private function get_curl_contents($url, $second = 5){
- if(!function_exists('curl_init')) die('php.ini未开启php_curl.dll');
- $c = curl_init();
- curl_setopt($c,curlopt_url,$url);
- $useragent=$_server['http_user_agent'];
- curl_setopt($c,curlopt_useragent,$useragent);
- curl_setopt($c,curlopt_header,0);
- curl_setopt($c,curlopt_timeout,$second);
- curl_setopt($c,curlopt_returntransfer, true);
- $cnt = curl_exec($c);
- curl_close($c);
- return $cnt;
- }
- static private function getsid() {
- $sid = time().(rand(0,9000)+10000);
- return $sid;
- }
- static private function getfileid($fileid,$seed) {
- $mixed = self::getmixstring($seed);
- $ids = explode("*",$fileid);
- unset($ids[count($ids)-1]);
- $realid = "";
- for ($i=0;$i $idx = $ids[$i];
- $realid .= substr($mixed,$idx,1);
- }
- return $realid;
- }
- static private function getmixstring($seed) {
- $mixed = "";
- $source = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz/\:._-1234567890";
- $len = strlen($source);
- for($i=0;$i $seed = ($seed * 211 + 30031) % 65536;
- $index = ($seed / 65536 * strlen($source));
- $c = substr($source,$index,1);
- $mixed .= $c;
- $source = str_replace($c, "",$source);
- }
- return $mixed;
- }
- }
- if(youkuflv::getyoukuflv("http://v.youku.com/v_show/id_xnjeyote4ntey_ev_1.html")){
- print_r( youkuflv::result() );
- }else{
- echo youkuflv::error();
- }
?>
-
复制代码
另外一种调用方法:
http://player.youku.com/player.php/sid/XMjI0MDIwNDc2/v.swf
http://player.youku.com/player.php/sid/{$id}/v.swf
|