简体   繁体   English

cURL connect()超时

[英]cURL connect() timed out

I've had my cURL implementation running successfully for the last few months without hiccups; 在过去的几个月里,我的cURL实现成功运行,没有打嗝; however, last week I suddenly started to have a problem with one specific website (www.viewmag.com). 然而,上周我突然开始遇到一个特定网站(www.viewmag.com)的问题。 I can visit the site (and have it resolve) perfectly in a browser, but cURL returns the following: 我可以在浏览器中完美地访问该网站(并让它解决),但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: 我的cURL如下:

$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? 有没有人想到为什么cURL失败以及为什么我能够在浏览器中访问这个网站,但却无法ping / cURL呢? Thanks in advance 提前致谢

  1. Maybe your server IP is banned on that site? 也许您的服务器IP在该网站上被禁止?

  2. Maybe try to set longer timeout? 也许尝试设置更长的超时? I visited that site and it works so slow, that you may need more than 5 seconds. 我访问了该网站,它的工作速度很慢,您可能需要超过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). 它适用于我的测试服务器(德国的数据中心)。

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. Ping可能不起作用,因为他们刚刚关闭了ping服务器,因此可以阻止分布式拒绝服务(DDOS)等攻击。 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. 您需要使用反复试验。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM