ApiConnect.class.php 2.8 KB
<?php
class Util_Video_ApiConnect
{
    private static $mDefaultOptions = array(
                                            CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0', 
                                            CURLOPT_CONNECTTIMEOUT => 0, 
                                            CURLOPT_TIMEOUT => 0.1
                                            );
    /**
     * 请在下面添加客户端消息头,防止API被视频网站封杀...
     */
    private static function _rand_agent()
    {
        $useragents = array(
                            'Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0', 
                            'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.15 (KHTML, like Gecko) Chrome/10.0.608.0 Safari/534.15', 
                            'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 6.1;)', 
                            'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 6.1;)', 
                            'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1;)', 
                            'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1;)', 
                            'Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1;)', 
                            'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 Safari/534.30', 
                            'Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.13', 
                            'Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.13'
                         );
        self::$mDefaultOptions[CURLOPT_USERAGENT] = array_rand($useragents, 1);
    }
    public static function get($url, $options = NULL)
    {
        self::_rand_agent();
        if($options === NULL)
        {
            // Use default options
            $options = self::$mDefaultOptions;
        }
        else
        {
            // Add default options
            $options = $options + self::$mDefaultOptions;
        }
        
        // The transfer must always be returned
        $options[CURLOPT_RETURNTRANSFER] = true;
        
        // Open a new remote connection
        $remote = curl_init($url);
        // Set connection options
        if(!curl_setopt_array($remote, $options))
        {
            return false;
        }
        // Get the response
        $response = curl_exec($remote);
        // Get the response information
        $code = curl_getinfo($remote, CURLINFO_HTTP_CODE);
        if($code && $code < 200 || $code > 299)
        {
            $error = $response;
        }
        elseif($response === false)
        {
            $error = curl_error($remote);
        }
        // Close the connection
        curl_close($remote);
        return $response;
    }

}

?>