class VideoSwfApi extends CApplicationComponent {
public $url;
public $alias = array('ku6', 'youku', 'tudou', 'sohu', 'letv', 'iqiyi', 'qq');
private static $instance;
public static function getInstance() {
if (is_null(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
public function fetch($url = null) {
if (is_null($url)) {
$url = $this->url;
}
if (empty($url)) {
return false;
}
$parse = parse_url($url);
if (!isset($parse['host']) || empty($parse['host'])) {
return false;
}
$alias = $this->_getAlias($parse['host']);
$func = $alias.'Api';
if (!method_exists($this, $func)) {
return $this->baiduApi($url);
}
return $this->$func($url);
}
/**
* 酷6
* @param unknown $url
* @return string
*/
public function ku6Api($url) {
return 'http://player.ku6.com/refer/'. $this->_filename($url) .'/v.swf';
}
/**
* 优酷
* @param unknown $url
* @return boolean|mixed
*/
public function youkuApi($url) {
$uri = 'https://openapi.youku.com/v2/videos/show_basic.json';
$params = array(
'client_id' => '840c098f672da0c6',
'video_url' => $url
);
$content = $this->_curl($uri.'?'.http_build_query($formdata));
if (empty($content)) {
return false;
}
$data = json_decode($content, true);
if (empty($data)) {
return false;
}
return $data['player'];
}
/**
* 土豆
* @param unknown $url
* @return boolean
*/
public function tudouApi($url) {
$uri = 'http://api.tudou.com/v6/video/info?app_key=6334753012795e9c&format=json&itemCodes='.$this->_filename($url);
$content = $this->_curl($uri);
if (empty($content)) {
return false;
}
$data = json_decode($content, true);
if (empty($data)) {
return false;
}
return $data['results'][0]['outerPlayerUrl'];
}
/**
* 搜狐
* @param unknown $url
* @return string|boolean
*/
public function sohuApi($url) {
$content = $this->_curl($url);
if (preg_match('/<meta[^>]+property="og:videosrc"[^>]+content="([^>]+)"[^>]*\/>/i', $content, $matches)) {
return trim($matches[1]);
}
return false;
}
/**
* 乐视网
* @param unknown $url
* @return string
*/
public function letvApi($url) {
return 'http://i7.imgs.letv.com/player/swfPlayer.swf?id='. $this->_filename($url) .'&autoplay=0';
}
private function _getAlias($host) {
$host = strtolower($host);
$arr = explode('.', $host);
if ($arr[0] == 'www') {
array_shift($arr);
}
array_pop($arr);
$alias = $this->alias;
foreach ($alias as $name) {
if (in_array($name, $arr)) {
return $name;
}
}
return false;
}
private function _build($stat, $url = null) {
return array('stat' => $stat, 'url' => $url);
}
private function _filename($url) {
$parseUrl = parse_url($url);
$path = $parseUrl['path'];
$pathInfo = pathinfo($path);
$filename = $pathInfo['filename'];
return $filename;
}
private function _curl($url) {
$referer = 'http://www.baidu.com';
$header = array(
'Host' => 'www.baidu.com',
'User-Agent' => 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36',
'X-Requested-With' => 'XMLHttpRequest',
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_REFERER, $referer);
$content = curl_exec($curl);
curl_close($curl);
return $content;
}
}