使用具有不同协议的代理的PHP cURL请求

时间:2015-06-25 11:48:38

标签: php http curl https proxy

如果我有2个代理:

$proxy1 = array('http', 'XXX.XXX.XXX.XXX','8080')$proxy2 = array('https','YYY.YYY.YYY.YYY','80')

我要求: $url1="http://somesite-1.net"$url2="https://somesite-2.net"

初始化多卷曲的代码:

foreach ($urls as $url) {
            // threads
            $ch = curl_init($url); 
            curl_setopt($ch, CURLOPT_AUTOREFERER, $_autoreferrer);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $_follow_loc);  
            curl_setopt($ch, CURLOPT_MAXREDIRS, $_max_redirects); 
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);    
            curl_setopt($ch, CURLOPT_HEADER, 0);                

            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $_connection_timeout);                   curl_setopt($ch, CURLOPT_TIMEOUT, $_curl_timeout);  
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,    true);
            curl_setopt($ch, CURLINFO_HTTP_CODE,        true);
            curl_setopt($ch, CURLOPT_PROXY,         $this->conf->getProxyIP().":".$this->conf->getProxyPort());     //PROXY       
            curl_setopt($ch, CURLOPT_USERAGENT,     $this->conf->getUserAgent());                                   
            if($referrer = $this->conf->getReferrer() !== false){                                                   //REFERRER
                curl_setopt($ch, CURLOPT_REFERER, $referrer);
            }



            $tasks[$url] = $ch;

            curl_multi_add_handle($cmh, $ch);
        }

我是否需要设置代理协议http或https来发送请求? 因为当我使用套接字检查代理时 - 我得到的消息是所有ok和代理都正常工作。但是当我使用cURL请求内容时 - 我得到的内容不是来自所有代理。我认为问题在于没有设置代理协议。我认为我需要在cURL中设置一些选项,但不知道到底是什么。

1 个答案:

答案 0 :(得分:0)

/**
 * get curl_setopt() params
 *
 * @since 2015/03/21 Saturday
 * @return array
 */
private function __optSets(){
    $this->__setCookie();
    // default:GET
    $options = array(
            CURLOPT_HEADER => 0,  
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_PORT => $this->port, 
            CURLOPT_REFERER => $this->reffer,
            CURLOPT_USERAGENT => $this->userAgent,
            CURLOPT_ENCODING =>'',
            CURLOPT_COOKIE =>$this->_cookie,
            CURLOPT_HTTPHEADER =>$this->_requestHeader, 
            CURLOPT_HTTPGET => true, 
            CURLOPT_POST => false,
            CURLOPT_CONNECTTIMEOUT => $this->_connect_timeout 
    );
    // if:POST
    if(self::HTTP_MEHTOD_POST === $this->method){
        $options[CURLOPT_HTTPGET] = false;
        $options[CURLOPT_POST] = true;
        $options[CURLOPT_POSTFIELDS] = $this->_poststr; ////default type:application/x-www-from-urlencoded
    }

    // if:HTTPS
    if ($this->_isHTTPS) { 
        $options [CURLOPT_PORT] = 443; // !Very important
        $options [CURLOPT_SSL_VERIFYPEER] = false;
        $options [CURLOPT_SSL_VERIFYHOST] = false;
    }

    // if:enableProxy
    if(true === $this->enableProxy){
        $options [CURLOPT_PROXY] = $this->_proxyIP;
        $options [CURLOPT_PROXYPORT] = $this->_proxyPort;
    }

    return $options;
}