欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

解析获取优酷视频真实下载地址的PHP源代码

程序员文章站 2023-09-04 10:51:07
复制代码 代码如下:
复制代码 代码如下:

<?php
//--调用方法/demo.php?url=http://v.youku.com/v_show/id_xmzkyoda2ntey.html
echo getyoukuflv($_get['url']);

function getyoukuflv($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;
}
?>