Api.class.php 2.52 KB
<?php
abstract class Util_Video_Api
{
    protected $id = '';
    private static $adapterApi = array();
    public static function loadData()
    {
        self::$adapterApi = array(
                                    '163', 
                                    '56', 
                                    'ku6', 
                                    '6', 
                                    'boosj', 
                                    'xiyou', 
                                    'cnlive', 
                                    'cntv', 
                                    'ifeng', 
                                    'joy', 
                                    'kankanews', 
                                    'letv', 
                                    'm1905', 
                                    'pptv', 
                                    'rbc', 
                                    'sina', 
                                    'smgbb', 
                                    'sohu', 
                                    'tudou', 
                                    'uusee', 
                                    'v1', 
                                    'xunlei',
                                    'yinyuetai', 
                                    'youku'
                                    );
    }
    public function getUrl($url)
    {
        if(empty($url))
        {
            throw new Util_Video_ApiException('url不能为空');
        }
        else
        {
            return $url;
        }
    }
    private static function regClass($name)
    {
        $class_prefix = 'Util_Video_Api_';
        $class = $class_prefix . $name;
        if((class_exists($class) && is_subclass_of($class, __CLASS__)))
        {
            return new $class();
        }
        else
        {
            throw new Util_Video_ApiException($class . '类不存在');
        }
    }
    
    public static function isVideoUrl($url)
    {
        self::loadData();
        $parseUrl = parse_url($url);
        foreach(self::$adapterApi as $api)
        {
            if(preg_match("/" . $api . "/is", $parseUrl['host']))
            {
                return $api;
            }
        }
        return '';
    }
    
    public static function autoGetData($url)
    {
        $api = self::isVideoUrl($url);
        $videoapi = self::regClass($api);
        $data = $videoapi->getData($url);
        if(!empty($data))
        {
            $data->url = $url;
        }
        return $data;
        return '';
    }
    abstract public function getData($url);
}

?>