【问题标题】:Proxy Authentication Required with cURLcURL 需要代理身份验证
【发布时间】:2012-04-07 00:14:44
【问题描述】:

我想使用 cURL 函数,但我在代理后面,所以我收到 HTTP/1.1 407 Proxy Authentication Required 错误...

这是我使用的php代码:

$proxy_user = 'Michiel';
$proxy_pass = 'mypassword';
$proxy_url = 'myproxyurl:port';
$proxy = true;

$service_url = "https://www.myapiurltocall.com";
$service_user = 'user:password:FO';
$service_pass = 'password';

$ch = curl_init($service_url);

// Set proxy if necessary
if ($proxy) {
    curl_setopt($ch, CURLOPT_PROXY, $proxy_url);
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy_user.':'.$proxy_pass);
    curl_setopt($ch, CURLOPT_PROXYPORT, 8080);
    curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_NTLM);
}

// Set service authentication
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, "{$service_user}:{$service_pass}");

// HTTP headers
$headers['Authorization'] = 'Basic ' . base64_encode("$proxy_user:$proxy_pass");

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);    
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_TIMEOUT, 15);

//WARNING: this would prevent curl from detecting a 'man in the middle' attack
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);

$data = curl_exec($ch);

而且我不知道我做错了什么......我该如何解决这个错误?

【问题讨论】:

  • 假设$proxy_user:$proxy_pass 是正确的值,如果您在办公室防火墙后面,则要使用的身份验证凭据将是管理员的凭据,而不是您自己的...我遇到了一些非常在我的工作环境中也有类似的情况,因为我的普通凭证没有必要的权限。
  • 是的,在办公室防火墙后面。 $proxy_user 和 $proxy 通行证是 100% 正确的(我可以使用我的 webmail 和带有这些凭据的通信器,所以......)我的电脑或网络的管理员你的意思是?
  • 好的,太好了!请添加它作为答案,所以我可以接受它:)

标签: php curl proxy


【解决方案1】:

尝试在你的函数中实现这个

   function send_string($data) {
//       pr($data);exit;

     $equi_input="Your values";
//  echo $equi_input; exit;
        $agent = ""//Agent Setting for Netscape  
        $url = ""; // URL to POST FORM. (Action of Form)
        //xml format
         $efx_request=strtoupper($equi_input);
      //echo  $efx_request;exit;
        $post_fields = "site_id=""&service_name=""&efx_request=$efx_request";
        $credentials = ;
        $headers = array("HTTP/1.1",
            "Content-Type: application/x-www-form-urlencoded",
            "Authorization: Basic " . $credentials
        );
        $fh = fopen('/tmp/test.txt', 'w') or die("can't open file"); //File to write the header information of the curl request.
        $ch = curl_init();    // Initialize a CURL session.
        curl_setopt($ch, CURLOPT_USERAGENT, $agent);
        curl_setopt($ch, CURLOPT_URL, $url);  // Pass URL as parameter.
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_STDERR, $fh);
        curl_setopt($ch, CURLOPT_POST, 1); // use this option to Post a form  
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); // Pass form Fields.  
        curl_setopt($ch, CURLOPT_VERBOSE, '1');
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, '1');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);  // grab URL and pass it to the variable.
        curl_close($ch);  // close curl resource, and free system resources.  
        fclose($fh);


   //echo '<pre>';print_r($array);echo '</pre>';exit;
        if (!empty($result)) {
            return $result;
        } else {
            echo "Curl Error" . curl_error($ch);
        }
    }

检查请求发送 200ok ...

如果您正在使用,请检查您的安全证书......

谢谢。

【讨论】:

  • 感谢您的回答,但我不确定如何在我当前的代码中实现这一点。你能帮帮我吗?
  • 我还传递了您的变量值,例如凭据、Url、授权.. 我在 $credentials 变量中传递授权.. 我在 postfield 中传递用户名、密码..,读取代码,我想它会帮助你。
  • 首先检查您的身份验证凭据,这可能会给您带来问题...检查..
  • 请注意上述代码中的一个错误:CURLOPT_SSL_VERIFYHOST 应该是一个整数参数,并且应该设置为 2(默认值)。有关详细信息,请参阅php.net/manual/en/function.curl-setopt.php。如果您将值设置为true,您实际上是在禁用主机检查,从而启用 MITM 攻击!
【解决方案2】:

假设$proxy_user:$proxy_pass 是正确的值,如果您在办公室防火墙后面,则要使用的身份验证凭据将是网络管理员的凭据,而不是您自己的...我在工作中遇到了非常相似的情况环境,对我来说就是这种情况,因为我的正常凭据没有必要的权限。但是,确保这种情况的最佳方法是确保代码在开放网络中工作,并注释您的代理设置。希望这会有所帮助!

【讨论】:

    【解决方案3】:

    这是我主要使用的:

    function curlFile($url,$proxy_ip,$proxy_port,$loginpassw)
    {
        //$loginpassw = 'username:password';
        //$proxy_ip = '192.168.1.1';
        //$proxy_port = '12345';
        //$url = 'http://www.domain.com';
    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);
        curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
        curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);
        curl_setopt($ch, CURLOPT_PROXYUSERPWD, $loginpassw);
        $data = curl_exec($ch);
        curl_close($ch);
    
        return $data;
    }
    

    【讨论】:

    • 好的,非常感谢!密码身份验证($url 本身)如何适合此设置?
    • 非常感谢您。这正是我想要构建的。你让我头疼不已。
    • 非常感谢,你让我摆脱了谷歌搜索的无限循环
    • 我们必须传递哪个用户名密码?
    • 这对我有帮助,谢谢
    【解决方案4】:

    这是我使用的,它完美地工作:

    $proxy = "1.1.1.1:12121";
    $useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
    $url = "http://www.google.pt/search?q=anonymous";
    $credentials = "user:pass"
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,15);
    curl_setopt($ch, CURLOPT_HTTP_VERSION,'CURL_HTTP_VERSION_1_1' );
    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
    curl_setopt($ch, CURLOPT_PROXY, $proxy);
    curl_setopt($ch, CURLOPT_PROXYUSERPWD,$credentials);
    curl_setopt($ch, CURLOPT_USERAGENT,$useragent);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
    $result=curl_exec ($ch);
    curl_close ($ch);
    

    【讨论】:

      猜你喜欢
      • 2021-10-10
      • 1970-01-01
      • 2017-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多