无法从本地主机通过 cURL 访问网站,但可以从托管服务器访问网站
我正在编写一个脚本,使用 PHP 5 和 cURL 从 wowarmory.com 提取 XML 数据:
$url = "http://www.wowarmory.com";
$userAgent = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12';
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$url);
$str = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
$str .= "Accept-Language: en-us,en;q=0.5\r\n";
$str .= "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n";
$str .= "Keep-Alive: 300\r\n";
$str .= "Connection: keep-alive\r\n";
curl_setopt($ch, CURLOPT_HTTPHEADER, array($str));
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 3000);
$xml = curl_exec($ch);
当我从托管 Web 服务器运行此脚本时,我会按预期获得 XML 响应。 但是当从本地主机网络服务器运行时,我什么也得不到。
我可以通过 cURL 从 localhost 获取任何其他网站( yahoo.com ,甚至 worldofwarcraft.com ),但不能获取 wowarmory.com 。 所以我知道 cURL 运行正常。
我使用以下版本的 PHP 和 cURL:
托管服务器:
- php 5.2.6
- cURL libcurl/7.16.1 OpenSSL/0.9.7e zlib/1.2.3
本地主机:
- php 5.2.6
- cURL libcurl/7.16.0 OpenSSL/0.9 .8i zlib/1.2.3
有什么想法吗?
编辑:本地主机正在运行 Windows XP SP3。 我可以通过网络浏览器访问 wowarmory.com。 Tracert 在第 13 跳开始超时(显然是从我的位置开始):
13 458 ms 529 ms 549 ms 0.so-6-0-0.BR1.LAX15.ALTER.NET [152.63.116.21]
14 476 ms 510 ms 488 ms 192.205.34.29
15 257 ms 279 ms 261 ms cr1.la2ca.ip.att.net [12.122.128.14]
16 242 ms 259 ms 249 ms gar5.la2ca.ip.att.net [12.122.128.25]
17 252 ms * 1691 ms 12.122.255.74
18 * 2361 ms 634 ms mdf001c7613r0003-gig-10-1.lax1.attens.net [12.129.193.242]
19 * * * Request timed out.
不幸的是,我不熟悉 tcptraceroute。
Windows 二进制版本的curl 不会为 http://www.wowarmory.com/ 返回任何内容,但是对于 http://www.yahoo.com/
我没有可用的 wget 。
编辑2:我可以很好地访问我的本地主机网站。 这只是我没有收到的curl 的响应。 我正在运行几乎默认的 XAMPP 安装(Windows xp 上的 apache 2 )。 所有这些都运行良好。
I'm writing a script that pulls XML data from wowarmory.com, using PHP 5 and cURL:
$url = "http://www.wowarmory.com";
$userAgent = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12';
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$url);
$str = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
$str .= "Accept-Language: en-us,en;q=0.5\r\n";
$str .= "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n";
$str .= "Keep-Alive: 300\r\n";
$str .= "Connection: keep-alive\r\n";
curl_setopt($ch, CURLOPT_HTTPHEADER, array($str));
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 3000);
$xml = curl_exec($ch);
When I run this from my hosted web server, I get the XML response as expected. But when running from my localhost web server, I get nothing.
I can get any other website via cURL from localhost ( yahoo.com, even worldofwarcraft.com ), but not wowarmory.com. So I know cURL is functioning properly.
I'm using the following versions of PHP and cURL:
Hosted Server:
- php 5.2.6
- cURL libcurl/7.16.1 OpenSSL/0.9.7e zlib/1.2.3
Localhost:
- php 5.2.6
- cURL libcurl/7.16.0 OpenSSL/0.9.8i zlib/1.2.3
Any ideas?
EDIT: Localhost is running Windows XP SP3. I can access wowarmory.com through a web browser. Tracert starts timing out at hop 13 ( from my location, obviously ):
13 458 ms 529 ms 549 ms 0.so-6-0-0.BR1.LAX15.ALTER.NET [152.63.116.21]
14 476 ms 510 ms 488 ms 192.205.34.29
15 257 ms 279 ms 261 ms cr1.la2ca.ip.att.net [12.122.128.14]
16 242 ms 259 ms 249 ms gar5.la2ca.ip.att.net [12.122.128.25]
17 252 ms * 1691 ms 12.122.255.74
18 * 2361 ms 634 ms mdf001c7613r0003-gig-10-1.lax1.attens.net [12.129.193.242]
19 * * * Request timed out.
I'm not familiar with tcptraceroute, unfortunately.
The windows binary version of curl doesn't return anything for http://www.wowarmory.com/ but does for http://www.yahoo.com/
I don't have wget available.
EDIT 2: I can access my localhosted website just fine. It's just the response from curl I don't receive. I'm running a pretty much default XAMPP install ( apache 2 on windows xp ). All of this works fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过网络浏览器甚至通过
curl
或wget
命令行实用程序从本地主机访问它吗?tcptraceroute
告诉您什么?如果网络浏览器和命令行实用程序都无法工作,但 tcptraceroute 可以工作(并且在某处没有显示透明代理),并且您位于 DSL 链接后面,则您的问题可能是是一个 PMTU 黑洞。 如果是这种情况,解决方法是 MSS 钳位。
Can you access it from your local host via a web browser or even via the
curl
orwget
command line utilities?What does
tcptraceroute
tell you?If neither a web browser nor the command line utility work, but
tcptraceroute
works (and does not show a transparent proxy somewhere in the way), and you are behind a DSL link, it is possible that your problem is a PMTU blackhole. If that is the case, the workaround is MSS clamping.api 可能会响应某个 URL(例如 nextUrl 或 currentUrl)。 如果您的 URL 是 localhost 或本地系统 IP 地址,API 响应如何以及在何处返回..? 因为 api 将响应 nextUrl() 或 currentUrl()。 所以请使用永久IP或域名进行工作。
api's probably give responses back to some URL (say nextUrl or currentUrl). if your URL is localhost or a local system IP address, how and where an api response back to..? Coz the api will response back to the nextUrl() or currentUrl(). So please use a permanent IP or domain name to work.