在过去的几个月里,我的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
Run Code Online (Sandbox Code Playgroud)
为了理智,我试图用两个不同的盒子ping网站,但每次ping都超时了.
方框1(Linux):
ping www.viewmag.com
PING www.viewmag.com (205.178.145.65) 56(84) bytes of data.
Run Code Online (Sandbox Code Playgroud)
方框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.
Run Code Online (Sandbox Code Playgroud)
我的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);
Run Code Online (Sandbox Code Playgroud)
有没有人想到为什么cURL失败以及为什么我能够在浏览器中访问这个网站,但却无法ping/cURL呢?提前致谢
也许您的服务器IP在该网站上被禁止?
也许尝试设置更长的超时?我访问了该网站,它的工作速度很慢,您可能需要超过5秒钟.
后来添加:
看起来您的服务器IP被禁止.
我试过这个(它的代码副本,更改在注释中):
<?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;
?>
Run Code Online (Sandbox Code Playgroud)
它适用于我的测试服务器(德国的数据中心).
| 归档时间: |
|
| 查看次数: |
36880 次 |
| 最近记录: |