php cURL 连接()超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16304924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cURL connect() timed out
提问by veerman
I've had my cURL implementation running successfully for the last few months without hiccups; however, last week I suddenly started to have a problem with one specific website (www.viewmag.com). I can visit the site (and have it resolve) perfectly in a browser, but cURL returns the following:
在过去的几个月里,我的 cURL 实现成功运行,没有出现任何问题;然而,上周我突然开始对一个特定的网站(www.viewmag.com)产生问题。我可以在浏览器中完美地访问该站点(并使其解析),但 cURL 返回以下内容:
* About to connect() to www.viewmag.com port 80 (#0)
* Trying 205.178.145.65... * Timeout
* connect() timed out!
* Closing connection #0
For sanity, I tried to ping the website with two different boxes, but each ping timed out.
为了理智,我尝试用两个不同的盒子 ping 网站,但每次 ping 都超时。
Box 1 (Linux):
框 1 (Linux):
ping www.viewmag.com
PING www.viewmag.com (205.178.145.65) 56(84) bytes of data.
Box 2 (Windows):
框 2(Windows):
ping www.viewmag.com
Pinging www.viewmag.com [205.178.145.65] with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.
My cURL is as follows:
我的卷曲如下:
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, 'http://www.viewmag.com');
curl_setopt ($ch, CURLOPT_USERAGENT, 'cURL crawler');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt ($ch, CURLOPT_AUTOREFERER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 2);
$html = curl_exec($ch);
Does anyone have any thoughts as to why cURL is failing and why I would be able to visit this site in a browser, but not be able to ping/cURL it? Thanks in advance
有没有人对 cURL 失败的原因有任何想法,以及为什么我可以在浏览器中访问该站点,但无法 ping/cURL 它?提前致谢
回答by Kamil
Maybe your server IP is banned on that site?
Maybe try to set longer timeout? I visited that site and it works so slow, that you may need more than 5 seconds.
也许您的服务器 IP 在该站点上被禁止?
也许尝试设置更长的超时时间?我访问了那个网站,它运行得很慢,你可能需要超过 5 秒。
Added later:
稍后补充:
Looks like your server IP is banned.
看起来你的服务器IP被禁止了。
I tried this (its copy of your code, changes are in comments):
我试过这个(它的代码副本,更改在注释中):
<?php
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, 'http://www.viewmag.com');
// I changed UA here
curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt ($ch, CURLOPT_AUTOREFERER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 2);
$html = curl_exec($ch);
// I added this
echo $html;
?>
and it works on my test server (data center in Germany).
它适用于我的测试服务器(德国的数据中心)。
回答by raidenace
In all probability they have increased security in their server. Some setting in server has changed to stop you from cUrling it. Try masquerading as a known user agent. Pinging might not work because they have just taken down the ping server so that attacks such as Distributed Denial of Service (DDOS) can be thwarted. Sadly at this point it cannot be determined what exact combination can or will make it work. You will need to employ trial and error.
他们很可能已经提高了服务器的安全性。服务器中的某些设置已更改以阻止您对其进行卷曲。尝试伪装成已知的用户代理。Ping 可能不起作用,因为他们刚刚关闭了 ping 服务器,以便可以阻止诸如分布式拒绝服务 (DDOS) 之类的攻击。遗憾的是,目前还无法确定哪种确切的组合可以或将使其发挥作用。您将需要进行反复试验。