我有以下 PHP 代码:
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $URL);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec ($curl);
echo $html;
结果如下:
对于像“http://www.google.com”这样的 $URL 是可以的
KO 像“http://localhost/index.html”这样的 $URL,并在我执行 var_dump 时返回 bool(false)
即使浏览器可以显示 http://localhost/index.html没问题。 我在本地主机上运行 xampp apache 服务器。
我不知道问题出在哪里。任何帮助将不胜感激。
最佳答案
您应该确保您的 Apache 监听正确的接口(interface)。我曾经在 Vist 机器上遇到过这个问题:Apache (XAMP) 仅在 IPv4 上监听,但在 Vista 上“localhost”默认解析为 IPv6 地址。
要禁用 IPv6 地址,请编辑 %system32%\drivers\etc\hosts 文件并删除::1 行。
关于php - Localhost URL 在浏览器中有效,但对于 PHP curl_exec 返回 bool(false),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4604845/