php file_get_contents - 连接超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4189973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:14:38  来源:igfitidea点击:

file_get_contents - Connection timed out

php

提问by Bruno

<?php
$a = file_get_contents('http://www.google.com');
echo $

Why is the browser returning this error?

为什么浏览器会返回这个错误?

Warning: file_get_contents(http://www.google.com) [function.file-get-contents]: failed to open stream: Connection timed out in /home/test.php on line 2

回答by Hamish

Mostly probably your server cannot connect to an external resource, for example, because of firewall restrictions.

很可能您的服务器无法连接到外部资源,例如,由于防火墙限制。

回答by pfrenssen

I had the same problem, couldn't download using file_get_contents(), but using curl on the command line for the same URL worked fine. Turned out it tried to connect over IPv6 which failed.

我遇到了同样的问题,无法使用 file_get_contents() 下载,但是在命令行上使用 curl 对同一 URL 工作正常。结果它试图通过 IPv6 连接失败。

I solved it by disabling IPv6 in my kernel parameters.

我通过在内核参数中禁用 IPv6 解决了这个问题。

回答by Brent Baisley

file_get_contentsdoes not work well at all with getting remote files and should not be used. It does not deal with slow network connections or redirects, and does not return error codes. You should use curlinstead to fetch remote files.

file_get_contents获取远程文件根本无法正常工作,不应使用。它不处理慢速网络连接或重定向,也不返回错误代码。您应该curl改为使用来获取远程文件。

There is an example in the manual for curl_exec: http://us3.php.net/manual/en/function.curl-exec.php

手册中有一个示例curl_exechttp: //us3.php.net/manual/en/function.curl-exec.php

回答by Mahedi Hasan

It might be server side issue, might be your server cannot communicate with other server remotely. You have to communicate with server administrator.

这可能是服务器端问题,可能是您的服务器无法与其他服务器远程通信。您必须与服务器管理员沟通。