<?php
02 //--调用方法http://xiaomizhou.net/demo/flv.php?url=http://v.youku.com/v_show/id_XMzg2OTQ3MjQw.html
03 echo fetch_youku_flv($_GET['url']);
04 function fetch_youku_flv($url){
05 preg_match("#id_(.*?)\.html#",$url,$out);
06 $id=$out[1];
07 $content=get_curl_contents('http://v.youku.com/player/getPlayList/VideoIDS/'.$id);
08 $data=json_decode($content);
09 foreach($data->data[0]->streamfileids AS $k=>$v){
10 $sid=getSid();
11 $fileid=getfileid($v,$data->data[0]->seed);
12 $one=($data->data[0]->segs->$k);
13 if($k == 'flv' || $k == 'mp4') return "http://f.youku.com/player/getFlvPath/sid/{$sid}_00/st/{$k}/fileid/{$fileid}?K={$one[0]->k}";
14 continue;
15 }
16 }
17 function get_curl_contents($url, $second = 5){
18 if(!function_exists('curl_init')) die('php.ini未开启php_curl.dll');
19 $c = curl_init();
20 curl_setopt($c,CURLOPT_URL,$url);
21 $UserAgent=$_SERVER['HTTP_USER_AGENT'];
22 curl_setopt($c,CURLOPT_USERAGENT,$UserAgent);
23 curl_setopt($c,CURLOPT_HEADER,0);
24 curl_setopt($c,CURLOPT_TIMEOUT,$second);
25 curl_setopt($c,CURLOPT_RETURNTRANSFER, true);
26 $cnt = curl_exec($c);
27 $cnt=mb_check_encoding($cnt,'utf-8')?iconv('gbk','utf-8//IGNORE',$cnt):$cnt; //字符编码转换
28 curl_close($c);
29 return $cnt;
30 }
31 function getSid() {
32 $sid = time().(rand(0,9000)+10000);
33 return $sid;
34 }
35 function getkey($key1,$key2){
36 $a = hexdec($key1);
37 $b = $a ^ 0xA55AA5A5;
38 $b = dechex($b);
39 return $key2.$b;
40 }
41 function getfileid($fileId,$seed) {
42 $mixed = getMixString($seed);
43 $ids = explode("*",$fileId);
44 unset($ids[count($ids)-1]);
45 $realId = "";
46 for ($i=0;$i < count($ids);++$i) {
47 $idx = $ids[$i];
48 $realId .= substr($mixed,$idx,1);
49 }
50 return $realId;
51 }
52 function getMixString($seed) {
53 $mixed = "";
54 $source = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\\:._-1234567890";
55 $len = strlen($source);
56 for($i=0;$i< $len;++$i){
57 $seed = ($seed * 211 + 30031) % 65536;
58 $index = ($seed / 65536 * strlen($source));
59 $c = substr($source,$index,1);
60 $mixed .= $c;
61 $source = str_replace($c, "",$source);
62 }
63 return $mixed;
64 }