根据各视频网站网址获取SWF链接的PHP实例仅供参考

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;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值