How to get the real flv file address of youku video with PHP,
The example in this article describes the method of obtaining the real flv file address of youku video with PHP. Share it with everyone for your reference. The specific analysis is as follows:
A webmaster asked me to help him make a real flv address that can automatically test the Youku video website. I sorted out this problem all afternoon and it is very good. You can refer to it.
This is a borrowed effort, it just grabs the content from friends’ websites, but it’s quite easy to use. The code is as follows:
Copy code The code is as follows:
$videourl='http://v.youku.com/v_show/id_XMjA5MjQ0OTQ0.html';
function get_content($url,$data){
if(is_array($data)){
$data = http_build_query($data, '', '&');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
return $result;
}
$str = get_content('http://share.pengyou.com/json.php?mod=usershare&act=geturlinfo',array('url'=>$videourl));
$str=json_decode($str);
var_dump($str);
?>
What came out of this test was only the swf file and not the flv file we wanted. Later, we improved it by referring to the writing method of a webmaster. The core code is as follows:
Copy code The code is as follows:
function fetch_youku_flv($url){
preg_match("#id_(.*?).html#",$url,$out);
$id=$out[1];
$content=get_curl_contents('http://v.youku.com/player/getPlayList/VideoIDS/'.$id);
$data=json_decode($content);
foreach($data->data[0]->streamfileids AS $k=>$v){
$sid=getSid();
$fileid=getfileid($v,$data->data[0]->seed);
$one=($data->data[0]->segs->$k);
if($k == 'flv' || $k == 'mp4') return "http://f.youku.com/player/getFlvPath/sid/{$sid}_00/st/{$k}/fileid/{$fileid}?K={$one[0]->k}";
continue;
}
}
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);
$cnt=mb_check_encoding($cnt,'utf-8')?iconv('gbk','utf-8//IGNORE',$cnt):$cnt; //字符编码转换
curl_close($c);
return $cnt;
}
function getSid() {
$sid = time().(rand(0,9000)+10000);
return $sid;
}
function getkey($key1,$key2){
$a = hexdec($key1);
$b = $a ^ 0xA55AA5A5;
$b = dechex($b);
return $key2.$b;
}
function getfileid($fileId,$seed) {
$mixed = getMixString($seed);
$ids = explode("*",$fileId);
unset($ids[count($ids)-1]);
$realId = "";
for ($i=0;$i < count($ids);++$i) {
$idx = $ids[$i];
$realId .= substr($mixed,$idx,1);
}
return $realId;
}
function getMixString($seed) {
$mixed = "";
$source = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/:._-1234567890";
$len = strlen($source);
for($i=0;$i< $len;++$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;
}
?>
调用方法,代码如下:
复制代码 代码如下:
$url='http://v.youku.com/v_show/id_XMzg2OTQ3MjQw.html';
echo fetch_youku_flv($url);
访问:
复制代码 代码如下:
http://xiaomizhou.net/demo/flv.php?url=http://v.youku.com/v_show/id_XMzg2OTQ3MjQw.html
输出的结果是:
复制代码 代码如下:
http://f.youku.com/player/getFlvPath/sid/138035737110468_00/st/flv/fileid/03000201004F97EF4A2E350467A09DB266E872-5522-225F-DC45-40B4E1F9BE49?K=c2a4327df2bb5a65261d40ea
This can be downloaded directly.
I hope this article will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/932069.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/932069.htmlTechArticleHow to get the real flv file address of youku video with PHP. This article describes how PHP gets the real flv file address of youku video. method. Share it with everyone for your reference. The specific analysis is as follows:...