使用PHP cURL代理时获取超时

时间:2015-12-03 23:48:36

标签: php

以下是相关代码:

$ch = curl_init();
curl_setopt(CURLOPT_URL, 'https://api.ipify.org?format=json');
curl_setopt(CURLOPT_PROXY, 'ip:port');
curl_setopt(CURLOPT_PROXYUSERPWD, 'user:pass');
$result = curl_exec($ch);
echo curl_error($ch);

我最常使用的代理和代理服务器。我实际上尝试过使用和不使用auth的各种来源的多个代理,但每次连接到代理时都会出现连接超时。

是否有配置设置阻止使用代理或我不知道的内容?这里的任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:3)

您需要对https网址使用CONNECT方法:

curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);

但有些代理不支持此功能。

答案 1 :(得分:0)

Sounds like an HTTPS issue to me. Try:

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);

It might also just be a slow URL, so try:

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0); 
curl_setopt($ch, CURLOPT_TIMEOUT, 400);//max seconds to allow cURL functions to run

If there's any redirects involved, you can also try:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

If that doesn't work, try testing more URLs and see if it's an https issue with every site you try, and whether you can recreate the same issue with http. Also check make sure you have PHP's error reporting enabled, and see if you're getting any errors or warnings. Try deliberately causing PHP to generate an error or warning to see if error reporting is working correctly.

答案 2 :(得分:0)

我解决了这个问题,只是想更新这个问题。

问题在于服务器配置。仅启用了最常见的出站端口,并且我在整个频谱中使用具有随机端口的代理。启用我需要的端口(你可以完全禁用限制)修复了这个问题。

因此,如果您在尝试连接代理时遇到同样的问题,请联系您的支持人员并询问有关出站端口的任何限制。