1. <?php 
  2. header("Content-Type:text/html;charset=gbk"); 
  3. //测试用地址 
  4. //$url = "http://v.youku.com/v_show/id_NTcy.html";//这是测试错误地址 
  5. $url = "http://v.youku.com/v_show/id_XMzA0OTIyNTcy.html"
  6. //$url="http://v.youku.com/v_playlist/f16733839o1p0.html"; 
  7. //$url="http://v.ku6.com/special/show_6561491/aqXLcYSzvYbGk6zs.html"; 
  8. //$url="http://v.ku6.com/show/JT1Bz_8Zkly3gVu2.html"; 
  9. //$url="http://www.tudou.com/programs/view/qaGXu9z3C_U/"; 
  10. //$url="http://www.tudou.com/playlist/p/a67624i108960861.html?&tid=19989,19990~_~"; 
  11. $video = new GetVideo($url"gbk"); //创建视频实例,第一个参数为视频网址,第二个参数为返回数据的编码格式,可不填,默认为utf-8 
  12. echo '$video->getArr():<br/><pre>'
  13. print_r($video->getArr()); //返回视频内容数组 
  14. echo '</pre><br/>'
  15. echo '$video->getTitle(): ' . $video->getTitle(); //返回视频标题 
  16. echo "<br/>"
  17. echo '$video->getDesc(): ' . $video->getDesc(); //返回视频描述 
  18. echo "<br/>"
  19. echo '$video->getImg(): ' . $video->getImg(); //返回视频小图地址 
  20. echo "<br/>"
  21. echo '<img src=' . $video->getImg() . ' />'
  22. echo "<br/>"
  23. echo '$video->getSwf(): ' . $video->getSwf(); //返回swf地址 
  24. echo "<br/>"
  25. echo '$video->getEmbed("500", "480"): <br/>' . $video->getEmbed("500""480"); //返回装配好的视频,参数分别为宽,高,可不填,默认为480,400 
  26. //write by zhwdtcwd 2011-12-14 
  27. class GetVideo { 
  28.     private $url = ""//视频地址 
  29.     private $encoding//显示的编码 
  30.     private $arr = array(); //视频内容装入数组 
  31.     //构造函数初始化数组$arr,调用私有方法getVideoArr获取视频信息, 
  32.     function __construct($_url$_encoding="utf-8") { 
  33.         try { 
  34.             $this->url = $_url
  35.             $this->encoding = $_encoding
  36.             $this->arr = $this->getVideoArr(); //调getVideoArr函数获得视频内容 
  37.         } catch (Exception $e) { 
  38.             echo $e->getMessage(); //如果获取视频失败则显示异常 
  39.             exit(); 
  40.         } 
  41.     } 
  42.     //私有方法getVideoArr获得视频内容 
  43.     private function getVideoArr() { 
  44.         if (strpos($this->url, "youku.com") > 0) { //判断传入的视频地址属于哪个网站,如优酷 
  45.             $videoArr = $this->captureYouku(); //调用相应的方法 
  46.         } elseif (strpos($this->url, "ku6.com") > 0) { 
  47.             $videoArr = $this->captureKu6(); 
  48.         } elseif (strpos($this->url, "tudou.com") > 0) { 
  49.             $videoArr = $this->captureTudou(); 
  50.         } else { 
  51.             throw new Exception("We just support youku,ku6 or tudou videos.Please check url"); //如传入网址非以上,则抛出异常 
  52.         } 
  53.         return $videoArr
  54.     } 
  55.     private function captureYouku() { 
  56.         $contents = @file_get_contents($this->url); //file_get_contents方法获取网页内容 
  57.         if (!preg_match("<html>"$contents)) {//判断获取的网页是否是正常html,有些网站屏蔽file_get_contents方法 
  58.             $contents = $this->curlGetContents(); //如果不是则调用私有方法curlGetContents获取网页 
  59.         } 
  60.         if (emptyempty($contents)) {//如果以上方法都不能获取内容,则该地址是无效地址,抛出异常 
  61.             throw new Exception("no contents,check url:" . $this->url); 
  62.         } 
  63.         //用正则匹配出标题,介绍,图片地址,swf地址 
  64.         preg_match("/\<meta name=\"title\" content=\"(.*)\"\>/"$contents$title); 
  65.         if (!emptyempty($title [1])) 
  66.             $videoArr['title'] = $title[1]; 
  67.         preg_match("/\<meta name=\"description\" content=\"([\s\S]*)\".*\>/U"$contents$description); 
  68.         if (!emptyempty($description [1])) 
  69.             $videoArr['desc'] = $description[1]; 
  70.         preg_match("/pic=(.*)\"/U"$contents$p_w_picpath); 
  71.         if (!emptyempty($p_w_picpath [1])) 
  72.             $videoArr['img'] = $p_w_picpath[1]; 
  73.         preg_match("/\<embed src=\"(.*)\.swf/"$contents$swf_scr); 
  74.         if (!emptyempty($swf_scr [1])) 
  75.             $videoArr['swf'] = $swf_scr[1] . ".swf"
  76.         //编码格式转换,优酷匹配除来的默认是utf-8,所以进行判断,目标编码非utf-8才进行转换。 
  77.         if (strtolower($this->encoding) != 'utf-8') { 
  78.             foreach ($videoArr as $key => $value) { 
  79.                 $new_arr[$key] = iconv('utf-8'$this->encoding, $value); 
  80.             } 
  81.             return $new_arr//如果进行了编码转换则返回 
  82.         } 
  83.         return $videoArr//没进行编码转换这返回 
  84.     } 
  85.     private function captureKu6() {//获取ku6网视频,同captureYouku方法类似 
  86.         $contents = @file_get_contents($this->url); 
  87.         if (!preg_match("<html>"$contents)) { 
  88.             $contents = $this->curlGetContents(); 
  89.         } 
  90.         if (emptyempty($contents)) { 
  91.             throw new Exception("no contents,check url:" . $this->url); 
  92.         } 
  93.         preg_match("/\<meta name=\"title\" content=\"(.*)\".*\/\>/"$contents$title); 
  94.         if (!emptyempty($title [1])) 
  95.             $videoArr['title'] = $title[1]; 
  96.         preg_match("/\<meta name=\"description\" content=\"([\s\S]*)\".*\>/U"$contents$description); 
  97.         if (!emptyempty($description [1])) 
  98.             $videoArr['desc'] = $description[1]; 
  99.         preg_match("/cover.?:.?\"(.*)\.jpg\",/U"$contents$p_w_picpath); 
  100.         if (!emptyempty($p_w_picpath [1])) 
  101.             $videoArr['img'] = $p_w_picpath[1] . ".jpg"
  102.         preg_match("/\<p class=\"con_A\"\>\<input class=\"text_A\" value=\"(.*)\.swf/"$contents$swf_scr); 
  103.         if (!emptyempty($swf_scr [1])) 
  104.             $videoArr['swf'] = $swf_scr[1] . ".swf"
  105.         if (strtolower($this->encoding) != 'gbk') { 
  106.             foreach ($videoArr as $key => $value) { 
  107.                 $new_arr[$key] = iconv('gbk'$this->encoding, $value); 
  108.             } 
  109.             return $new_arr
  110.         } 
  111.         return $videoArr
  112.     } 
  113.     private function captureTudou() {//获取土豆网视频,同captureYouku方法类似 
  114.         $contents = @file_get_contents($this->url); 
  115.         if (!preg_match("<html>"$contents)) { 
  116.             $contents = $this->curlGetContents(); 
  117.         } 
  118.         if (emptyempty($contents)) { 
  119.             throw new Exception("no contents,check url:" . $this->url); 
  120.         } 
  121.         preg_match("/\<title\>(.*)_/U"$contents$title); 
  122.         if (!emptyempty($title [1])) 
  123.             $videoArr['title'] = $title[1]; 
  124.         preg_match("/desc = \"(.*)\"/U"$contents$description); 
  125.         if (!emptyempty($description [1])) { 
  126.             $desc = str_replace('\n'""$description[1]); 
  127.             $videoArr['desc'] = $desc
  128.         } 
  129.         preg_match("/pic.*http:\/\/(.*).jpg/U"$contents$p_w_picpath); 
  130.         if (!emptyempty($p_w_picpath [1])) 
  131.             $videoArr['img'] = 'http://' . $p_w_picpath[1] . '.jpg'
  132.         preg_match("/.code = \'(.*)\'/"$contents$swf_scr); 
  133.         preg_match("/defaultIid = ([0-9]+)/"$contents$swf_scr2); 
  134.         if (!emptyempty($swf_scr [1]) && !emptyempty($swf_scr2 [1])) { 
  135.             $videoArr['swf'] = 'http://www.tudou.com/l/' . $swf_scr[1] . '/&iid=' . $swf_scr2[1] . '/v.swf'
  136.         } 
  137.         if (!emptyempty($swf_scr [1]) && emptyempty($swf_scr2 [1])) { 
  138.             $videoArr['swf'] = 'http://www.tudou.com/v/' . $swf_scr[1] . '/v.swf'
  139.         } 
  140.         if (strtolower($this->encoding) != 'gbk') { 
  141.             foreach ($videoArr as $key => $value) { 
  142.                 $new_arr[$key] = iconv('gbk'$this->encoding, $value); 
  143.             } 
  144.             return $new_arr
  145.         } 
  146.         return $videoArr
  147.     } 
  148.     private function curlGetContents() {//用curl方法获取网页内容 
  149.         //模拟浏览器报头 
  150.         $agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; ' . 
  151.                 'rv:1.9.1) Gecko/20090624 Firefox/3.5 (.NET CLR ' . 
  152.                 '3.5.30729)'
  153.         $ch = curl_init(); 
  154.         curl_setopt($ch, CURLOPT_URL, $this->url); 
  155.         curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
  156.         curl_setopt($ch, CURLOPT_HEADER, 0); 
  157.         curl_setopt($ch, CURLOPT_ENCODING, "gzip"); 
  158.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  159.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
  160.         curl_setopt($ch, CURLOPT_FAILONERROR, 1); 
  161.         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 8); 
  162.         curl_setopt($ch, CURLOPT_TIMEOUT, 8); 
  163.         $result = curl_exec($ch); 
  164.         curl_close($ch); 
  165.         return $result
  166.     } 
  167.     function getArr() { 
  168.         return $this->arr; 
  169.     } 
  170.     function getTitle() { 
  171.         return $this->arr['title']; 
  172.     } 
  173.     function getDesc() { 
  174.         return $this->arr['desc']; 
  175.     } 
  176.     function getImg() { 
  177.         return $this->arr['img']; 
  178.     } 
  179.     function getSwf() { 
  180.         return $this->arr['swf']; 
  181.     } 
  182.     function getEmbed($width="480"$height="400") {//返回装配好的视频 
  183.         $embedded = '<embed src="' . $this->arr['swf'] . '" allowFullScreen="true" quality="high" width="' . $width . '" height="' . $height . '" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash"></embed>'
  184.         return $embedded
  185.     } 
  186. ?>