- <?php
- header("Content-Type:text/html;charset=gbk");
- //测试用地址
- //$url = "http://v.youku.com/v_show/id_NTcy.html";//这是测试错误地址
- $url = "http://v.youku.com/v_show/id_XMzA0OTIyNTcy.html";
- //$url="http://v.youku.com/v_playlist/f16733839o1p0.html";
- //$url="http://v.ku6.com/special/show_6561491/aqXLcYSzvYbGk6zs.html";
- //$url="http://v.ku6.com/show/JT1Bz_8Zkly3gVu2.html";
- //$url="http://www.tudou.com/programs/view/qaGXu9z3C_U/";
- //$url="http://www.tudou.com/playlist/p/a67624i108960861.html?&tid=19989,19990~_~";
- $video = new GetVideo($url, "gbk"); //创建视频实例,第一个参数为视频网址,第二个参数为返回数据的编码格式,可不填,默认为utf-8
- echo '$video->getArr():<br/><pre>';
- print_r($video->getArr()); //返回视频内容数组
- echo '</pre><br/>';
- echo '$video->getTitle(): ' . $video->getTitle(); //返回视频标题
- echo "<br/>";
- echo '$video->getDesc(): ' . $video->getDesc(); //返回视频描述
- echo "<br/>";
- echo '$video->getImg(): ' . $video->getImg(); //返回视频小图地址
- echo "<br/>";
- echo '<img src=' . $video->getImg() . ' />';
- echo "<br/>";
- echo '$video->getSwf(): ' . $video->getSwf(); //返回swf地址
- echo "<br/>";
- echo '$video->getEmbed("500", "480"): <br/>' . $video->getEmbed("500", "480"); //返回装配好的视频,参数分别为宽,高,可不填,默认为480,400
- //write by zhwdtcwd 2011-12-14
- class GetVideo {
- private $url = ""; //视频地址
- private $encoding; //显示的编码
- private $arr = array(); //视频内容装入数组
- //构造函数初始化数组$arr,调用私有方法getVideoArr获取视频信息,
- function __construct($_url, $_encoding="utf-8") {
- try {
- $this->url = $_url;
- $this->encoding = $_encoding;
- $this->arr = $this->getVideoArr(); //调getVideoArr函数获得视频内容
- } catch (Exception $e) {
- echo $e->getMessage(); //如果获取视频失败则显示异常
- exit();
- }
- }
- //私有方法getVideoArr获得视频内容
- private function getVideoArr() {
- if (strpos($this->url, "youku.com") > 0) { //判断传入的视频地址属于哪个网站,如优酷
- $videoArr = $this->captureYouku(); //调用相应的方法
- } elseif (strpos($this->url, "ku6.com") > 0) {
- $videoArr = $this->captureKu6();
- } elseif (strpos($this->url, "tudou.com") > 0) {
- $videoArr = $this->captureTudou();
- } else {
- throw new Exception("We just support youku,ku6 or tudou videos.Please check url"); //如传入网址非以上,则抛出异常
- }
- return $videoArr;
- }
- private function captureYouku() {
- $contents = @file_get_contents($this->url); //file_get_contents方法获取网页内容
- if (!preg_match("<html>", $contents)) {//判断获取的网页是否是正常html,有些网站屏蔽file_get_contents方法
- $contents = $this->curlGetContents(); //如果不是则调用私有方法curlGetContents获取网页
- }
- if (emptyempty($contents)) {//如果以上方法都不能获取内容,则该地址是无效地址,抛出异常
- throw new Exception("no contents,check url:" . $this->url);
- }
- //用正则匹配出标题,介绍,图片地址,swf地址
- preg_match("/\<meta name=\"title\" content=\"(.*)\"\>/", $contents, $title);
- if (!emptyempty($title [1]))
- $videoArr['title'] = $title[1];
- preg_match("/\<meta name=\"description\" content=\"([\s\S]*)\".*\>/U", $contents, $description);
- if (!emptyempty($description [1]))
- $videoArr['desc'] = $description[1];
- preg_match("/pic=(.*)\"/U", $contents, $p_w_picpath);
- if (!emptyempty($p_w_picpath [1]))
- $videoArr['img'] = $p_w_picpath[1];
- preg_match("/\<embed src=\"(.*)\.swf/", $contents, $swf_scr);
- if (!emptyempty($swf_scr [1]))
- $videoArr['swf'] = $swf_scr[1] . ".swf";
- //编码格式转换,优酷匹配除来的默认是utf-8,所以进行判断,目标编码非utf-8才进行转换。
- if (strtolower($this->encoding) != 'utf-8') {
- foreach ($videoArr as $key => $value) {
- $new_arr[$key] = iconv('utf-8', $this->encoding, $value);
- }
- return $new_arr; //如果进行了编码转换则返回
- }
- return $videoArr; //没进行编码转换这返回
- }
- private function captureKu6() {//获取ku6网视频,同captureYouku方法类似
- $contents = @file_get_contents($this->url);
- if (!preg_match("<html>", $contents)) {
- $contents = $this->curlGetContents();
- }
- if (emptyempty($contents)) {
- throw new Exception("no contents,check url:" . $this->url);
- }
- preg_match("/\<meta name=\"title\" content=\"(.*)\".*\/\>/", $contents, $title);
- if (!emptyempty($title [1]))
- $videoArr['title'] = $title[1];
- preg_match("/\<meta name=\"description\" content=\"([\s\S]*)\".*\>/U", $contents, $description);
- if (!emptyempty($description [1]))
- $videoArr['desc'] = $description[1];
- preg_match("/cover.?:.?\"(.*)\.jpg\",/U", $contents, $p_w_picpath);
- if (!emptyempty($p_w_picpath [1]))
- $videoArr['img'] = $p_w_picpath[1] . ".jpg";
- preg_match("/\<p class=\"con_A\"\>\<input class=\"text_A\" value=\"(.*)\.swf/", $contents, $swf_scr);
- if (!emptyempty($swf_scr [1]))
- $videoArr['swf'] = $swf_scr[1] . ".swf";
- if (strtolower($this->encoding) != 'gbk') {
- foreach ($videoArr as $key => $value) {
- $new_arr[$key] = iconv('gbk', $this->encoding, $value);
- }
- return $new_arr;
- }
- return $videoArr;
- }
- private function captureTudou() {//获取土豆网视频,同captureYouku方法类似
- $contents = @file_get_contents($this->url);
- if (!preg_match("<html>", $contents)) {
- $contents = $this->curlGetContents();
- }
- if (emptyempty($contents)) {
- throw new Exception("no contents,check url:" . $this->url);
- }
- preg_match("/\<title\>(.*)_/U", $contents, $title);
- if (!emptyempty($title [1]))
- $videoArr['title'] = $title[1];
- preg_match("/desc = \"(.*)\"/U", $contents, $description);
- if (!emptyempty($description [1])) {
- $desc = str_replace('\n', "", $description[1]);
- $videoArr['desc'] = $desc;
- }
- preg_match("/pic.*http:\/\/(.*).jpg/U", $contents, $p_w_picpath);
- if (!emptyempty($p_w_picpath [1]))
- $videoArr['img'] = 'http://' . $p_w_picpath[1] . '.jpg';
- preg_match("/.code = \'(.*)\'/", $contents, $swf_scr);
- preg_match("/defaultIid = ([0-9]+)/", $contents, $swf_scr2);
- if (!emptyempty($swf_scr [1]) && !emptyempty($swf_scr2 [1])) {
- $videoArr['swf'] = 'http://www.tudou.com/l/' . $swf_scr[1] . '/&iid=' . $swf_scr2[1] . '/v.swf';
- }
- if (!emptyempty($swf_scr [1]) && emptyempty($swf_scr2 [1])) {
- $videoArr['swf'] = 'http://www.tudou.com/v/' . $swf_scr[1] . '/v.swf';
- }
- if (strtolower($this->encoding) != 'gbk') {
- foreach ($videoArr as $key => $value) {
- $new_arr[$key] = iconv('gbk', $this->encoding, $value);
- }
- return $new_arr;
- }
- return $videoArr;
- }
- private function curlGetContents() {//用curl方法获取网页内容
- //模拟浏览器报头
- $agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; ' .
- 'rv:1.9.1) Gecko/20090624 Firefox/3.5 (.NET CLR ' .
- '3.5.30729)';
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $this->url);
- curl_setopt($ch, CURLOPT_USERAGENT, $agent);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_ENCODING, "gzip");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($ch, CURLOPT_FAILONERROR, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 8);
- curl_setopt($ch, CURLOPT_TIMEOUT, 8);
- $result = curl_exec($ch);
- curl_close($ch);
- return $result;
- }
- function getArr() {
- return $this->arr;
- }
- function getTitle() {
- return $this->arr['title'];
- }
- function getDesc() {
- return $this->arr['desc'];
- }
- function getImg() {
- return $this->arr['img'];
- }
- function getSwf() {
- return $this->arr['swf'];
- }
- function getEmbed($width="480", $height="400") {//返回装配好的视频
- $embedded = '<embed src="' . $this->arr['swf'] . '" allowFullScreen="true" quality="high" width="' . $width . '" height="' . $height . '" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash"></embed>';
- return $embedded;
- }
- }
- ?>