当前位置:首页 > 网络编程 > php > 正文

php 获取优酷视频的真实地址(2014.6月新算法)

上个礼拜发现优酷改版了,各种过滤优酷广告的插件都失效了,于是我百度了一下(谷歌也不能用了)发现优酷改算法了,在ckplayer论坛发现有人在6月25号发了个php 的优酷代理文件,下载下来发现,能用但只能获取mp4格式的视频地址,而且php还加密了,没办法查看源码,后来通过微盾解密发现其中的源码,结合以前自己写的一个优酷视频解析类。。。。

感谢    3shi大大 具体分析请见 3shi大大的文章  优酷视频真实地址解析  (当然现在不能用了,主要看分析

 

ps.  新算法是从别人那里解密出来的所以有可能存在错误,当然也没有注释,不过我试了几个视频都可以解析。

 

下面是源码:

文件名为:youku.class.php

复制代码
  1 <?php
  2 
  3 class Youku {
  4 
  5     const USER_AGENT = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36";
  6     const REFERER = "http://www.youku.com";
  7     const FORM_ENCODE = "GBK";
  8     const TO_ENCODE = "UTF-8";
  9     private static $base = "http://v.youku.com/player/getPlaylist/VideoIDS/";
 10     private static $source = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\\:._-1234567890";
 11     private static $sz = '-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1';
 12     private static $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
 13 
 14     public static function parse($url){
 15         preg_match("#id\_([\w=]+)#", $url, $matches); //id里可以有=号
 16         if (empty($matches)){
 17             $html = self::_cget($url);
 18             preg_match("#videoId2\s*=\s*\'(\w+)\'#", $html, $matches);
 19             if(!$matches) return false;
 20         }
 21         //根据you vid 获取相应的视频地址
 22         return self::_getYouku(trim($matches[1]));
 23     }
 24     /**
 25      * [_cget curl获取数据]
 26      * @param  [type]  $url     [url地址]
 27      * @param  boolean $convert [是否转换编码]
 28      * @param  integer $timeout [超时时间]
 29      * @return [type]           [description]
 30      */
 31     public static function _cget($url,$convert=false,$timeout=10){
 32         $ch=curl_init($url);
 33         curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
 34         curl_setopt($ch,CURLOPT_TIMEOUT,$timeout);
 35         curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
 36         curl_setopt($ch,CURLOPT_USERAGENT,self::USER_AGENT);
 37         curl_setopt($ch,CURLOPT_REFERER,self::REFERER);       
 38         curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1); //跟随301跳转
 39         curl_setopt($ch,CURLOPT_AUTOREFERER,1); //自动设置referer              
 40         $res=curl_exec($ch);
 41         curl_close($ch);
 42         if($convert){
 43             $res=mb_convert_encoding($res,self::TO_ENCODE,self::FORM_ENCODE);
 44         }
 45         return $res;
 46     }    
 47 
 48     //start 获得优酷视频需要用到的方法
 49     private static function getSid(){
 50         $sid = time().(mt_rand(0,9000)+10000);
 51         return $sid;
 52     }
 53 
 54     private static function getKey($key1,$key2){
 55         $a = hexdec($key1);
 56         $b = $a ^0xA55AA5A5;
 57         $b = dechex($b);
 58         return $key2.$b;
 59     }
 60 
 61     private static function getFileid($fileId,$seed){
 62         $mixed = self::getMixString($seed);
 63         $ids = explode("*",rtrim($fileId,'*')); //去掉末尾的*号分割为数组
 64         $realId = "";
 65         for ($i=0;$i<count($ids);$i++){
 66             $idx = $ids[$i];
 67             $realId .= substr($mixed,$idx,1);
 68         }  
 69         return $realId;
 70     } 
 71 
 72     private static function getMixString($seed){
 73         $mixed = "";
 74         $source = self::$source;
 75         $len = strlen($source);
 76         for($i=0;$i<$len;$i++){
 77             $seed = ($seed * 211 + 30031)%65536;
 78             $index = ($seed / 65536 * strlen($source));
 79             $c = substr($source,$index,1);
 80             $mixed .= $c;
 81             $source = str_replace($c,"",$source);
 82         }
 83         return $mixed;
 84     }
 85 
 86     private static function yk_d($a){
 87         if (!$a) {
 88             return '';
 89         }
 90         $f = strlen($a);
 91         $b = 0;
 92         $str = self::$str;
 93         for ($c = ''; $b < $f;) {
 94             $e = self::charCodeAt($a, $b++) & 255;
 95             if ($b == $f) {
 96                 $c .= self::charAt($str, $e >> 2);
 97                 $c .= self::charAt($str, ($e & 3) << 4);
 98                 $c .= '==';
 99                 break;
100             }
101             $g = self::charCodeAt($a, $b++);
102             if ($b == $f) {
103                 $c .= self::charAt($str, $e >> 2);
104                 $c .= self::charAt($str, ($e & 3) << 4 | ($g & 240) >> 4);
105                 $c .= self::charAt($str, ($g & 15) << 2);
106                 $c .= '=';
107                 break;
108             }
109             $h = self::charCodeAt($a, $b++);
110             $c .= self::charAt($str, $e >> 2);
111             $c .= self::charAt($str, ($e & 3) << 4 | ($g & 240) >> 4);
112             $c .= self::charAt($str, ($g & 15) << 2 | ($h & 192) >> 6);
113             $c .= self::charAt($str, $h & 63);
114         }
115         return $c;
116     }
117     private static function yk_na($a){
118         if (!$a) {
119             return '';
120         }
121 
122         $h = explode(',', self::$sz);
123         $i = strlen($a);
124         $f = 0;
125         for ($e = ''; $f < $i;) {
126             do {
127                 $c = $h[self::charCodeAt($a, $f++) & 255];
128             } while ($f < $i && -1 == $c);
129             if (-1 == $c) {
130                 break;
131             }
132             do {
133                 $b = $h[self::charCodeAt($a, $f++) & 255];
134             } while ($f < $i && -1 == $b);
135             if (-1 == $b) {
136                 break;
137             }
138             $e .= self::fromCharCode($c << 2 | ($b & 48) >> 4);
139             do {
140                 $c = self::charCodeAt($a, $f++) & 255;
141                 if (61 == $c) {
142                     return $e;
143                 }
144                 $c = $h[$c];
145             } while ($f < $i && -1 == $c);
146             if (-1 == $c) {
147                 break;
148             }
149             $e .= self::fromCharCode(($b & 15) << 4 | ($c & 60) >> 2);
150             do {
151                 $b = self::charCodeAt($a, $f++)  
             
                        
             
上一篇:php 数组的常用函数 下一篇:最后一页