Sina.class.php 4.27 KB
<?php
class Util_Video_Api_Sina extends Util_Video_Api
{
    private $htmlurl = "http://video.sina.com.cn/playlist/{var}.html";
    private $swfurl = "http://you.video.sina.com.cn/api/sinawebApi/outplayrefer.php/vid={vid}_{uid}/s.swf";
    private $infourl = "http://v.iask.com/v_play.php?vid={vid}&uid={uid}&pid=0&tid=0&plid=4004&prid=ja_7_0&isAuto=0&referrer=&ran={rand}&r=p.you.video.sina.com.cn";
    private $imgurl = "http://interface.video.sina.com.cn/interface/common/getVideoImage.php?vid={vid}";
    //新浪网
    const SINA_VIDEO_PID = 70; // 新浪视频PID
    private static $sina_video_api = 'http://10.55.40.246/api/sinaVideoApi.php?pid=%s&data=json&url=%s'; // 新浪视频接口
    public function getData($url)
    {
        $data = array ();
        $file = @file_get_contents ( $url );
        preg_match_all ( "/<script[^>]*?>(.*?)<\\/script>/is", $file, $out );
        $matchInfo = $out [1] [0];
        if (! $matchInfo) 
        {
            return $this->getViolentData($url);
        }
        $pic_start = strpos ( $matchInfo, 'pic:' );
        $pic_end = strpos ( $matchInfo, ',', $pic_start );
        $pic_str = substr ( $matchInfo, $pic_start, ($pic_end - $pic_start) );
        $pic = substr ( $pic_str, 6, - 1 );
 
        $title_start = strpos ( $matchInfo, 'title:' );
        $title_end = strpos ( $matchInfo, ',', $title_start );
        $title_str = substr ( $matchInfo, $title_start, ($title_end - $title_start) );
        $title = substr ( $title_str, 7, - 1 );
        
        $flash_start = strpos ( $matchInfo, 'swfOutsideUrl:' );
        if($flash_start<=0)
        {
            return $this->getViolentData($url);
        }
        $flash_end = strpos ( $matchInfo, ',', $flash_start );
        $flash_str = substr ( $matchInfo, $flash_start, ($flash_end - $flash_start) );
        $flash = substr ( $flash_str, 15, - 1 );
        $data = new Util_Video_Data($url);
        $data->swf = $flash;
        $data->title = $title;
        $data->thumb = $pic;
        return $data;
    }
    
    private function getViolentData($url)
    {
        $idandswf = $this->getid($url);
        $infourl = str_ireplace("{vid}", $idandswf['vid'], $this->infourl);
        $infourl = str_ireplace("{uid}", $idandswf['uid'], $infourl);
        $infourl = str_ireplace("{rand}", rand(10000000, 99999999) / 10000000, $infourl);
        $infodat = Util_Video_ApiConnect::get($infourl);
        if(!function_exists("simplexml_load_file"))
        {
            throw new Util_Video_ApiException('simplexml_load_file 不存在');
        }
        $infodat = preg_replace("/\<\!\[CDATA\[(.*?)\]\]\>/ies", "base64_encode('$1')", $infodat);
        $infodat = @simplexml_load_string($infodat);
        if($infodat == null)
        {
            throw new Util_Video_ApiException('数据解析失败');
        }
        $imgurl = str_ireplace("{vid}", $idandswf['vid'], $this->imgurl);
        $imgurl = Util_Video_ApiConnect::get($imgurl);
        $imgurl = preg_replace("/^.*=/is", "", $imgurl);
        $dataobj = new Util_Video_Data($this->id);
        $dataobj->swf = $idandswf['swf'];
        $dataobj->thumb = $imgurl;
        $dataobj->time = (string) $infodat->timelength;
        $dataobj->title = base64_decode($infodat->vname);
        $dataobj->source = '来自于sina.com';
        return $dataobj;
    }
    private function getid($url)
    {
        $url = $this->getUrl($url);
        if(!preg_match("/^http:\/\/.*/si", $url))
        {
            $url = str_ireplace("{var}", $url, $this->htmlurl);
        }
        $htmldat = Util_Video_ApiConnect::get($url);
        if(preg_match_all("@vid\s*:\s*\'(.*)\'*uid\s*:\s*\'(.*)\'@siU", $htmldat, $out, 2))
        {
            $vid = substr($out[0][1], 0, strpos($out[0][1], "'"));
            $uid = $out[0][2];
        }
        else
        {
        	$id = substr(strrchr($url,'#'), 1);
        	if(is_numeric($id))
        	{
        		$vid = $id;
        		$uid = 6;
        	}
        	else
        	{
        		throw new Util_Video_ApiException("Can not parse the character");
        	} 
        }
        $swf = str_ireplace("{vid}", $vid, $this->swfurl);
        $swf = str_ireplace("{uid}", $uid, $swf);
        $this->id = $url;
        return array(
            "swf" => $swf, 
            "vid" => $vid, 
            "uid" => $uid
        );
    }
}

?>