【发布时间】:2014-06-22 15:18:36
【问题描述】:
我在我的 Centos 机器上使用以下代码来获取 URL 响应:
<?php
ini_set('display_errors', 'On');
$homepage = file_get_contents('http://www.google.com/');
echo $homepage;
?>
返回错误:
“警告:file_get_contents(http://www.google.com/): 无法打开流:第 3 行 testFGC.php 中的连接超时”
我检查了php.ini 文件。一切看起来都很好。 iptables 和 ip6tables 已关闭。
我尝试使用curl 代替file_get_contents,但效果不佳。
但是 bash 命令行上的正常 curl 工作绝对正常(返回 html 内容)。
curl www.google.com
可能是什么原因?即使 bash curl 正在工作,防火墙规则也会成为问题吗?谢谢。
【问题讨论】:
-
换个网站试试,会成功的..
-
不适用于任何外部网站。正在为所有本地网络 IP 工作。
-
您说 curl 不起作用,然后启用 CURLOPT_VERBOSE 并发布 curl 详细日志。
$ch=curl_init();$tmp=tmpfile();curl_setopt_array($ch,array(CURLOPT_VERBOSE=>1,CURLOPT_STDERR=>$tmp,CURLOPT_URL=>'http://google.com'));curl_exec($ch);/*php bug 76268 workaround*/rewind($tmp);var_dump(stream_get_contents($tmp));curl_close($ch);fclose($tmp);,你得到了什么?
标签: php bash curl file-get-contents