PHP 다양한 동영상 컬렉션 패키지, Youku, Tudou, Tencent Video, 56, Sohu Video, Kuliu, Sina Video, LeTV...
나인 jigwang.com
-
- /**
- * 동영상
- *
- * @package
- * @version 1.3
- * @copyright 2011 - 2013
- *
- * 사용법
- * require_once "VideoUrlParser.class.php";
- * $urls[] = "http://v.youku.com/v_show/id_XMjI4MDM4NDc2.html";
- * $urls[] = "http://www.tudou.com/playlist/p/l13087099.html";
- * $urls[] = "http://www.tudou.com/programs/view /ufg-A3tlcxk/";
- * $urls[] = "http://v.ku6.com/special/show_4926690/Klze2mhMeSK6g05X.html";
- * $urls[] = "http:// www.56.com/u68/v_NjI2NTkxMzc.html";
- * $urls[] = "http://www.letv.com/ptv/vplay/1168109.html";
- * $urls[] = "http://video.sina.com.cn/v/b/46909166-1290055681.html";
- *
- * foreach($urls as $url){
- * $info = VideoUrlParser ::parse($url);
- * //var_dump($info);
- * echo "{ $info['title']}";
- * echo "
";
- * echo $info['object'];
- * echo "< br />";
- * }
- *
- *
- *
- * //优酷
- * http://v.youku.com/v_show/id_XMjU0NjY4OTEy.html
- *
- *
- * //酷六
- * http://v.ku6.com/special/show_3917484/x0BMXAbgZdQS6FqN.html
- *
- *
- * //土豆
- * http://www.tudou.com/playlist/p /a65929.html?iid=74905844
- *
- *
- * //56
- * http://www.56.com/u98/v_NTkyODY2NTU.html
- *
- *
- * //새로운浪播客
- * http://video.sina .com.cn/v/b/46909166-1290055681.html
- *
- *
- * //乐视
- * http://www.letv.com/ptv/vplay/1168109.html
- *
- */
-
- class VideoUrlparser
- {
- const USER_AGENT = "Mozilla/5.0(Windows; 유; 윈도우 NT 5.1; en-US) AppleWebKit/534.10(KHTML, like Gecko)
- Chrome/8.0.552.224 Safari/534.10";
- const CHECK_URL_VALID = "/(youku.com|tudou.com|ku6.com|56.com |letv.com|video.sina.com.cn|(my.)?tv.sohu.com|v.qq.com)/";
-
- /**
- * 구문 분석
- *
- * @param string $url
- * @param 혼합 $createObject
- * @static
- * @access public
- * @return void
- */
- 정적 공용 함수 구문 분석($url='', $createObject=true){
- $lowerurl = strtolower($url);
- preg_match(self::CHECK_URL_VALID, $lowerurl, $matches);
- if (!$matches) return false;
-
- switch($matches[1]){
- case 'youku.com':
- $data = self::_parseYouku($url);
- break;
- 케이스 'tudou.com':
- $data = self::_parseTudou($url);
- break;
- 케이스 'ku6.com':
- $data = self ::_parseKu6($url);
- break;
- case '56.com':
- $data = self::_parse56($url);
- break;
- case 'letv. com':
- $data = self::_parseLetv($url);
- break;
- 케이스 'video.sina.com.cn':
- $data = self::_parseSina($url );
- break;
- 케이스 'my.tv.sohu.com':
- 케이스 'tv.sohu.com':
- 케이스 'sohu.com':
- $data = self ::_parseSohu($url);
- break;
- case 'v.qq.com':
- $data = self::_parseQq($url);
- break;
- 기본값:
- $data = false;
- }
-
- if($data && $createObject) $data['object'] = "" ;
- $data 반환;
- }
- /**
- * 텐센트 동영상
- * http://v.qq.com/cover/o/o9tab7nuu0q3esh.html?vid=97abu74o4w3_0
- * http://v.qq.com/play/97abu74o4w3.html
- * http://v.qq.com/cover/d/dtdqyd8g7xvoj0o.html
- * http://v.qq.com/cover/d/dtdqyd8g7xvoj0o/9SfqULsrtSb.html
- * http: //imgcache.qq.com/tencentvideo_v1/player/TencentPlayer.swf?_v=20110829&vid=97abu74o4w3&autoplay=1&list=2&showcfg=1&tpid=23&title=First Live&adplay=1&cid=o9tab7nuu0q3esh
- */
- 비공개 정적 함수 _parseQq($url){
- if(preg_match("//play//", $url)){
- $html = self ::_fget($url);
- preg_match("/url=[^"] /", $html, $matches);
- if(!$matches); return false;
- $url = $matches[0];
- }
- preg_match("/vid=([^_] )/", $url, $matches);
- $vid = $matches[1];
- $html = self::_fget($url);
- // 쿼리
- preg_match("/flashvarss=s"([^;] )/s", $html, $matches);
- $ 쿼리 = $matches[1];
- if(!$vid){
- preg_match("/vids?=s?vids?||s?"(w )";/i", $html, $ 일치);
- $vid = $matches[1];
- }
- $query = str_replace('" vid "', $vid, $query);
- parse_str($query, $output );
- $data['img'] = "http://vpic.video.qq.com/{$$output['cid']}/{$vid}_1.jpg";
- $ data['url'] = $url;
- $data['title'] = $output['title'];
- $data['swf'] = "http://imgcache.qq.com /tencentvideo_v1/player/TencentPlayer.swf?".$query;
- return $data;
- }
-
- /*
- * 优酷网
- * http://v.youku .com/v_show/id_XMjI4MDM4NDc2.html
- * http://player.youku.com/player.php/sid/XMjU0NjI2Njg4/v.swf
- */
- 비공개 정적 함수 _parseYouku($url){
- preg_match("#id_(w )#", $url, $matches);
-
- if (empty($matches)){
- preg_match("#v_playlist/#", $url , $mat);
- if(!$mat) return false;
-
- $html = self::_fget($url);
-
- preg_match("#videoId2s*=s* '(w )'#", $html, $matches);
- if(!$matches) return false;
- }
-
- $link = "http://v.youku.com /player/getPlayList/VideoIDS/{$matches[1]}/timezone/ 08/version/5/source/out?password=&ran=2513&n=3";
-
- $retval = self::_cget( $link);
- if ($retval) {
- $json = json_decode($retval, true);
-
- $data['img'] = $json['data'][0 ]['logo'];
- $data['title'] = $json['data'][0]['title'];
- $data['url'] = $url;
- $data['swf'] = "http://player.youku.com/player.php/sid/{$matches[1]}/v.swf";
-
- // echo "< ;사전>"; var_dump($data);
-
- return $data;
- } else {
- return false;
- }
- }
-
- /**
- * 투도우
- * http://www.tudou.com/programs/view/Wtt3FjiDxEE/
- * http://www.tudou.com/v/Wtt3FjiDxEE/v.swf
- *
- * http://www.tudou.com/playlist/p/a65718.html?iid=74909603
- * http://www.tudou.com/l/G5BzgI4lAb8/&iid=74909603/v.
- */
- 비공개 정적 함수 _parseTudou($url){
- preg_match("#view/([-w] )/#", $url, $matches);
-
- if (empty($matches)) {
- if (strpos($url, "/playlist/") == false) return false;
-
- if(strpos($url, 'iid=') !== false){
- $quarr = 폭발("iid=", $lowerurl);
- if (empty($quarr[1])) return false;
- }elseif(preg_match("#p/l(d ).#", $lowerurl , $quarr)){
- if (empty($quarr[1])) return false;
- }
-
- $html = self::_fget($url);
- $html = iconv("GB2312", "UTF-8", $html);
-
- preg_match("/lid_codes=slcodes=s['"]([^'"] )/s", $html, $matches);
- $icode = $matches[1];
-
- preg_match("/iids=s.*?||s(d )/sx", $html, $matches);
- $iid = $matches[1];
-
- preg_match("/listDatas=s([{.*}])/sx", $html, $matches);
-
- $find = array("/n/", '/s/', "/:[^d"]w [^,]* ,/i", "/({|,)(w ):/");
- $replace = array("", "", ':"",', '\1"\2":' );
- $str = preg_replace($find, $replace, $matches[1]);
- //var_dump($str);
- $json = json_decode($str);
- / /var_dump($json);exit;
- if(is_array($json) || is_object($json) && !empty($json)){
- foreach($json을 $val로) {
- if ($val->iid == $iid) {
- break;
- }
- }
- }
-
- $data['img'] = $val-> pic;
- $data['title'] = $val->title;
- $data['url'] = $url;
- $data['swf'] = "http:// www.tudou.com/l/{$icode}/&iid={$iid}/v.swf";
-
- return $data;
- }
-
- $host = "www .tudou.com";
- $path = "/v/{$matches[1]}/v.swf";
-
- $ret = self::_fsget($path, $host);
-
- if (preg_match("#nLocation: (.*)n#", $ret, $mat)) {
- parse_str(parse_url(urldecode($mat[1]), PHP_URL_QUERY));
-
- $data['img'] = $snap_pic;
- $data['title'] = $title;
- $data['url'] = $url;
- $data ['swf'] = "http://www.tudou.com/v/{$matches[1]}/v.swf";
-
- return $data;
- }
- return false;
- }
-
- /**
- * Ku6.com
- * http://v.ku6.com/film/show_520/3X93vo4tIS7uotHg.html
- * http://v.ku6.com/special/show_4926690/Klze2mhMeSK6g05X.html
- * http://v.ku6.com/show/7US-kDXjyKyIInDevhpwHg...html
- * http://player.ku6.com/refer/3X93vo4tIS7uotHg/v.swf
- */
- 개인 정적 함수 _parseKu6($url){
- if(preg_match("/show_/", $url)){
- preg_match("#/([-w] ).html#", $url, $matches);
- $url = "http://v.ku6.com/fetchVideo4Player/{$matches[1 ]}.html";
- $html = self::_fget($url);
-
- if ($html) {
- $json = json_decode($html, true);
- if(!$json) return false;
-
- $data['img'] = $json['data']['picpath'];
- $data['title'] = $json[ 'data']['t'];
- $data['url'] = $url;
- $data['swf'] = "http://player.ku6.com/refer/{$ match[1]}/v.swf";
-
- return $data;
- } else {
- return false;
- }
- }elseif(preg_match("/show// ", $url, $matches)){
- $html = self::_fget($url);
- preg_match("/ObjectInfos?=s?([^n]*)};/si", $html, $matches);
- $str = $matches[1];
- // img
- preg_match("/covers?:s?"([^"] )"/", $str , $matches);
- $data['img'] = $matches[1];
- // 제목
- preg_match("/title"?s?:s?"([^"] ) "/", $str, $matches);
- $jsstr = "{"title":"{$matches[1]}"}";
- $json = json_decode($jsstr, true);
- $data['title'] = $json['title'];
- // url
- $data['url'] = $url;
- // 쿼리
- preg_match(" /"(vid=[^"] )"sname="flashVars"/s", $html, $matches);
- $query = str_replace("&", '&', $matches[1]) ;
- preg_match("///player.ku6cdn.com[^"'] /", $html, $matches);
- $data['swf'] = 'http:'.$matches[0 ].'?'.$query;
-
- return $data;
- }
- }
-
- /**
- * 56.com
- * http://www.56.com/u73/v_NTkzMDcwNDY.html
- * http://player.56.com/v_NTkzMDcwNDY.swf
- */
- 개인 정적 함수 _parse56($ url){
- preg_match("#/v_(w ).html#", $url, $matches);
-
- if (empty($matches)) return false;
-
- $link="http://vxml.56.com/json/{$matches[1]}/?src=out";
- $retval = self::_cget($link);
-
- if ($retval) {
- $json = json_decode($retval, true);
-
- $data['img'] = $json['info']['img'];
- $data['title'] = $json['info']['Subject'];
- $data['url'] = $url;
- $data['swf'] = "http: //player.56.com/v_{$matches[1]}.swf";
-
- return $data;
- } else {
- return false;
- }
- }
-
- /**
- * 르TV
- * http://www.letv.com/ptv/vplay/1168109.html
- * http://www.letv.com/player/x1168109.swf
- */
- 개인 정적 함수 _parseLetv($url){
- $html = self::_fget($url);
- preg_match("#http: //v.t.sina.com.cn/([^'"]*)#", $html, $matches);
- parse_str(parse_url(urldecode($matches[0]), PHP_URL_QUERY));
- preg_match("#vplay/(d )#", $url, $matches);
- $data['img'] = $pic;
- $data['title'] = $title;
- $data['url'] = $url;
- $data['swf'] = "http://www.letv.com/player/x{$matches[1]}.swf";
-
- return $data;
- }
-
- // 搜狐TV http://my.tv.sohu.com/u/vw/5101536
- 개인 정적 함수 _parseSohu($url){
- $html = self::_fget($url);
- $html = iconv("GB2312", "UTF-8", $html);
- preg_match_all("/og:(?:제목 |image|videosrc)"scontent="([^"] )"/s", $html, $matches);
- $data['img'] = $matches[1][1];
- $data['title'] = $matches[1][0];
- $data['url'] = $url;
- $data['swf'] = $matches[1][2] ;
- $data 반환;
- }
-
- /*
- * 新浪播客
- * http://video.sina.com.cn/v/b/48717043-1290055681. html
- * http://you.video.sina.com.cn/api/sinawebApi/outplayrefer.php/vid=48717043_1290055681_PUzkSndrDzXK l1lHz2stqkP7KQNt6nki2O0u1ehIwZYQ0/XM5GdatoG5ynSA9kEqDhAQJA4dPkm0 x4/s.swf
- */
- 개인 정적 함수 _parseSina( $url){
- preg_match("/(d )(?:-|_)(d )/", $url, $matches);
- $url = "http://video.sina.com .cn/v/b/{$matches[1]}-{$matches[2]}.html";
- $html = self::_fget($url);
- preg_match("/videos? :s?([^ $find = array("/n/", "/s*/", "/'/", "/ {([^:,] ):/", "/,([^:] ):/", "/:[^d"]w [^,]*,/i");
- $replace = array('', '', '"', '{"\1":', ',"\1":', ':"",');
- $str = preg_replace($find, $replace, $matches[1]);
- $arr = json_decode($str, true);
-
- $data['img'] = $arr['pic'];
- $ data['title'] = $arr['title'];
- $data['url'] = $url;
- $data['swf'] = $arr['swfOutsideUrl'];
-
- return $data;
- }
-
- /*
- * 통过 file_get_contents 获取内容
- */
- 개인 정적 함수 _fget($url=''){
- if(!$url) return false;
- $html = file_get_contents($url);
- // 判断是否gzip压缩
- if($dehtml = self::_gzdecode($html))
- return $dehtml;
- else
- return $html;
- }
-
- /*
- * 통过 fsockopen 获取内容
- */
- 개인 정적 함수 _fsget($ 경로='/', $host='', $user_agent=''){
- if(!$path || !$host) false 반환;
- $user_agent = $user_agent ? $user_agent : self::USER_AGENT;
-
- $out = <<GET $path HTTP/1.1
- 호스트: $host
- 사용자 에이전트: $user_agent
- 수락: text/html,application/xhtml xml,application/xml;q=0.9,*/*;q=0.8
- 수락 언어: zh-cn,zh;q=0.5
- 수락 문자 집합 : GB2312,utf-8;q=0.7,*;q=0.7rnrn
- HEADER;
- $fp = @fsockopen($host, 80, $errno, $errstr, 10);
- if ( !$fp) false 반환;
- if(!fputs($fp, $out)) return false;
- while ( !feof($fp) ) {
- $html .= fgets($fp, 1024);
- }
- fclose($fp);
- // 判断是否gzip压缩
- if($dehtml = self::_gzdecode($html))
- return $dehtml;
- else
- return $html;
- }
- /*
- * 通过 컬 获取内容
- */
- 개인 정적 함수 _cget($url='', $ user_agent=''){
- if(!$url) return;
- $user_agent = $user_agent ? $user_agent : self::USER_AGENT;
- $ch = 컬_init();
- 컬_setopt($ch, CURLOPT_URL, $url);
- 컬_setopt($ch, CURLOPT_HEADER, 0);
- if(strlen($user_agent)) 컬_setopt($ch, CURLOPT_USERAGENT, $user_agent);
- ob_start();
- 컬_exec($ch);
- $html = ob_get_contents();
- ob_end_clean();
- if(curl_errno($ch)){
- curl_close($ch);
- return false;
- }
- curl_close($ch);
- if(!is_string($html) || !strlen($html)){
- return false;
- }
- return $html;
- // 判断是否gzip压缩
- if($dehtml = self::_gzdecode($html))
- return $dehtml;
- else
- return $html;
- }
-
- 개인 정적 함수 _gzdecode($data) {
- $len = strlen ( $data );
- if ($len < 18 || strcmp ( substr ( $data, 0, 2 ) , "x1fx8b" )) {
- null을 반환합니다. // GZIP 형식 아님(RFC 1952 참조)
- }
- $method = ord ( substr ( $data, 2, 1 ) ); // 압축 방법
- $flags = ord ( substr ( $data, 3, 1 ) ); // 플래그
- if ($flags & 31 != $flags) {
- // 예약된 비트가 설정됨 -- RFC 1952에서는 허용되지 않음
- return null;
- }
- // 참고 : $mtime은 음수일 수 있습니다(PHP 정수 제한)
- $mtime = unpack ( "V", substr ( $data, 4, 4 ) );
- $mtime = $mtime [1];
- $ xfl = substr( $data, 8, 1 );
- $os = substr( $data, 8, 1 );
- $headerlen = 10;
- $extralen = 0;
- $extra = "";
- if ($flags & 4) {
- // 헤더에 EXTRA 데이터 접두사가 붙은 2바이트 길이
- if ($len - $headerlen - 2 < 8) {
- return false ; // 잘못된 형식
- }
- $extralen = unpack ( "v", substr ( $data, 8, 2 ) );
- $extralen = $extralen [1];
- if ($len - $headerlen - 2 - $extralen < 8) {
- false를 반환합니다. // 잘못된 형식
- }
- $extra = substr ( $data, 10, $extralen );
- $headerlen = 2 $extralen;
- }
-
- $filenamelen = 0;
- $filename = "";
- if ($flags & 8) {
- // 헤더의 C 스타일 문자열 파일 NAME 데이터
- if ($len - $headerlen - 1 < 8) {
- false를 반환합니다. // 잘못된 형식
- }
- $filenamelen = strpos ( substr ( $data, 8 $extralen ), chr ( 0 ) );
- if ($filenamelen === false || $len - $headerlen - $filenamelen - 1 < 8) {
- false를 반환합니다. // 잘못된 형식
- }
- $filename = substr ( $data, $headerlen, $filenamelen );
- $headerlen = $filenamelen 1;
- }
-
- $commentlen = 0 ;
- $comment = "";
- if ($flags & 16) {
- // 헤더의 C 스타일 문자열 COMMENT 데이터
- if ($len - $headerlen - 1 < 8) {
- false를 반환합니다. // 잘못된 형식
- }
- $commentlen = strpos ( substr ( $data, 8 $extralen $filenamelen ), chr ( 0 ) );
- if ($commentlen === false || $len - $headerlen - $commentlen - 1 < 8) {
- false를 반환합니다. // 잘못된 헤더 형식
- }
- $comment = substr ( $data, $headerlen, $commentlen );
- $headerlen = $commentlen 1;
- }
-
- $headercrc = "";
- if ($flags & 1) {
- // 헤더에 CRC32의 2바이트(최하위 순서)
- if ($len - $headerlen - 2 < 8) {
- 거짓을 반환; // 잘못된 형식
- }
- $calccrc = crc32 ( substr ( $data, 0, $headerlen ) ) & 0xffff;
- $headercrc = unpack ( "v", substr ( $data, $headerlen, 2 ) );
- $headercrc = $headercrc [1];
- if ($headercrc != $calccrc) {
- return false; // 잘못된 헤더 CRC
- }
- $headerlen = 2;
- }
-
- // GZIP FOOTER - PHP의 제한으로 인해 음수가 됩니다.
- $datacrc = unpack ( "V" , substr ( $data, - 8, 4 ) );
- $datacrc = $datacrc [1];
- $isize = unpack ( "V", substr ( $data, - 4 ) );
- $isize = $isize [1];
-
- // 압축 해제 수행:
- $bodylen = $len - $headerlen - 8;
- if ($bodylen < 1) {
- // 절대 이런 일이 일어나서는 안 됩니다 - 구현 버그!
- null 반환;
- }
- $body = substr ( $data, $headerlen, $bodylen );
- $data = "";
- if ($bodylen > 0) {
- switch ($method) {
- case 8 :
- // 현재 유일하게 지원되는 압축 방법:
- $data = gzinflate ( $body );
- break;
- default :
- // 알 수 없는 압축 방법
- return false;
- }
- } else {
- //...
- }
-
- if ($isize != strlen ( $data ) || crc32 ( $data ) != $datacrc) {
- // 형식이 잘못되었습니다! 길이 또는 CRC가 일치하지 않습니다!
- false 반환;
- }
- $data 반환;
- }
- }
-
- /*
-
- $url = "http://v.youku.com/v_show/id_XNjIxNjUyOTky.html";
-
- $obj = new VideoUrlparser();
-
- $data = $obj->parse($url);
-
- var_dump($data);
-
- */
-
코드 복사
|