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

标签 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"

初始化多 curl 的代码:

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);
        }

是否需要设置代理协议(protocol)http或https来发送请求? 因为当我使用套接字检查代理时 - 我收到消息说一切正常并且代理正在工作。但是当我使用 cURL 请求内容时 - 我不是从所有代理获取内容。我认为问题在于未设置代理协议(protocol)。我想我需要在 cURL 中设置一些选项,但不知 Prop 体是什么。

最佳答案

/**
 * 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;
}

关于使用具有不同协议(protocol)的代理的 PHP cURL 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31049511/

相关文章:

php - 分页不正确显示页码 Codeigniter

php - 我无法在 XAMPP 中打开数据库

javascript - 如何保持变量的值?

python - 在curl下载python脚本时添加延迟,然后通过管道执行脚本?

php - 在 php 中选择的多个复选框的数据未存储在数据库中

php - WAMP Server V 2.5 图标为橙色,无响应且无菜单

django - 为什么 Web 框架通过 FastCGI/SCGI 而不是 HTTP 提供服务?

ruby - 具有自定义 IP 的域上的 HTTP 请求

php - 使用 PHP 在 Curl 中将 .PEM 和 .KEY 作为字符串传递

php - file_get_contents() 和 Curl