cURL connect()超时 [英] cURL connect() timed out
问题描述
我的cURL实现在过去几个月没有打嗝的情况下成功运行;然而,上周我突然开始有一个特定的网站(www.viewmag.com)的问题。我可以在浏览器中完美地访问网站(并让它解决),但cURL返回以下内容:
()到www.viewmag.com端口80(#0)
*尝试205.178.145.65 ... *超时
* connect()超时!
*正在关闭连接#0
Box 1(Linux):
ping www.viewmag.com
PING www.viewmag.com(205.178.145.65)56(84)字节的数据。
方块2(Windows):
ping www.viewmag.com
Pinging www.viewmag.com [205.178.145.65]带32字节数据:
请求超时。
请求超时。
请求超时。
请求超时。
我的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);
有没有人对cURL为什么会失败以及为什么我会访问这个网站有任何想法在浏览器中,但是不能ping / cURL呢?提前感谢
-
也许您的服务器IP被禁止在该网站上? p>
-
也许尝试设置更长的超时时间?我访问了该网站,运行缓慢,可能需要5秒以上。
稍后添加:
看起来您的服务器IP已被禁止。
我试过这个(它的代码的副本,更改是在评论):
<?php
$ ch = curl_init();
curl_setopt($ ch,CURLOPT_URL,'http://www.viewmag.com');
//我改变了UA这里
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);
//我添加了这个$ b $ b echo $ html;
?>
,它在我的测试服务器(德国的数据中心)上工作。
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:
* 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.
Box 1 (Linux):
ping www.viewmag.com
PING www.viewmag.com (205.178.145.65) 56(84) bytes of data.
Box 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
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.
Added later:
Looks like your server IP is banned.
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).
这篇关于cURL connect()超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!