ApiConnect.class.php 4.75 KB
<?php
class Util_Product_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 => 2,
    										CURLOPT_DNS_USE_GLOBAL_CACHE => false
                                            );
    /**
     * 请在下面添加客户端消息头,防止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;
        }
        
        if(isset($options[CURLOPT_FOLLOWLOCATION]))
        {
        	$isFollow = true;
        	unset($options[CURLOPT_FOLLOWLOCATION]);
        }
        else
        {
        	$isFollow = false;
        }
        // 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
        if($isFollow)
        {
        	$response = self::curl_exec_follow($remote);
        }
        else
        {
        	$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;
    }
    
    private static function curl_exec_follow( $ch,  &$maxredirect = 50) 
   	{
    	$mr = $maxredirect === null ? 5 : intval($maxredirect);
    	if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off'))
        {
    		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0);
    		curl_setopt($ch, CURLOPT_MAXREDIRS, $mr);
    	} 
    	else 
    	{
    		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
    		if ($mr > 0)
    		 {
    			$newurl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    
    			$rch = curl_copy_handle($ch);
    			curl_setopt($rch, CURLOPT_HEADER, true);
    			curl_setopt($rch, CURLOPT_NOBODY, true);
    			curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
    			curl_setopt($rch, CURLOPT_RETURNTRANSFER, true);
    			do {
    				curl_setopt($rch, CURLOPT_URL, $newurl);
    				$header = curl_exec($rch);
    				if (curl_errno($rch)) 
    				{
    					$code = 0;
    				} 
    				else 
    				{
    					$code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
    					if ($code == 301 || $code == 302) 
    					{
    						preg_match('/Location:(.*?)\n/', $header, $matches);
    						$newurl = trim(array_pop($matches));
    					}
    					else 
    					{
    						$code = 0;
    					}
    				}
    			} while ($code && --$mr);
    			curl_close($rch);
    			if (!$mr) 
    			{
    				if ($maxredirect === null) 
    				{
    					return false;
    				} 
    				else
    				 {
    					$maxredirect = 0;
    				}
    				return false;
    			}
    			curl_setopt($ch, CURLOPT_URL, $newurl);
    		}
    	}
    	return curl_exec($ch);
    }

}

?>