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

yipeiwu_com5年前PHP代码库
复制代码 代码如下:

<?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;
}
?>

相关文章

PHP计算近1年的所有月份

PHP计算近1年的所有月份

话不多说,请看代码:  $z = date('Y-m'); $a = date('Y-m', strtotime('-12 months')); $begin = new...

使用php语句将数据库*.sql文件导入数据库

最简单的php语句把数据库*.sql文件导入数据库 复制代码 代码如下: $sql=file_get_contents("text.sql"); //把SQL语句以字符串读入$sql $...

crontab无法执行php的解决方法

本文分析了crontab无法执行php的解决方法。分享给大家供大家参考,具体如下: 用crontab跑php程序时,如何去调试,各人有各人的方法。我也有套方法,看一下,我是如何解决cro...

在html文件中也可以执行php语句的方法

在apache的conf目录中修改文件httpd.conf。 搜索AddType application/ 找到前面没有#的那条,在下面添加AddType application/x-h...

利用PHP制作简单的内容采集器的原理分析

前几天做了个小说连载的程序,因为怕更新麻烦,顺带就写了个采集器,采集八路中文网的,功能比较简单,不能自定义规则,不过大概思路都在里面了,自定义规则可以自己来扩展。   用php来做采集器...