PHP fopen 函数超时?
知道为什么 fopen 会在我的服务器上的文件上超时并且我知道 url 是正确的吗?
更新:抱歉,我应该提到这是在 php.ini 中。 代码是:
fopen($url, 'r');
如果我输入文件的相对路径,它就可以工作,但如果 $url 是我的服务器中的 url,则不行(但它适用于 google.com)。 谢谢您的帮助。
阿莱特尼克的回答是正确的。 仅当我通过以太网接口访问自己的服务器文件时,才会出现此问题。 我怎样才能解决这个问题? 我需要能够从以太网接口访问该文件,因为 url 是动态加载的(它是从 wordpress cms 生成的,因此从技术上讲,该 url 并不作为文件存在于我的服务器上)
any idea why fopen would timeout for a file if it is on my server and I know the url is correct?
update: sorry, i should have mentioned this is in php.
the code is:
fopen($url, 'r');
It works if i put in a relative path for the file, but not if $url is a url in my server (but it works for google.com). Thanks for the help.
Alaitnik's answer was right. The problem only appears when i access my own server files through the ethernet interface. How can I fix this? I need to be able to access the file from the ethernet interface because the url loads dynamically (it's generated from a wordpress cms, so the url doesn't technically exist as a file on my server)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您似乎正在尝试使用 HTTP 协议从同一服务器上运行的程序从您自己的服务器下载文件?
如果是这样,超时问题可能与 Web 服务器或网络配置有关。 超时通常只会发生,因为:
例如,您的本地防火墙规则可能只允许访问
www.example。 com
如果这些查询来自以太网接口,但本地建立的连接将尝试通过环回接口。It appears that you're trying to download a file from your own server using the HTTP protocol from a program running on that same server?
If so, the timeout problem is likely to be web server or network configuration related. Timeouts normally only happen because either:
For example, it may be that your local firewall rules only permit access to
www.example.com
if those queries come from the ethernet interface, but a locally made connection would try to go via the loopback interface.也许您的“allow_url_fopen”设置为“关闭”
检查你的 php.ini 文件或 phpinfo()
maybe your "allow_url_fopen" is set to "Off"
check your php.ini file or phpinfo()
如果您尝试获取 URL 的 HTML,我建议使用curl 而不是fopen。
fopen 最适合与本地文件一起使用,因为它不“知道”如何处理网络资源的特性。
If you are trying to get the HTML of a URL, I suggest using curl instead of fopen.
fopen is best used with local files, coz it does not "know" how to deal with the idiosyncrasies of a network resource.
查看fopen文档上的注释。 里面有很多金子。
Check the comments on the documentation of fopen. There's a whole lot of gold in there.
我花了很长时间才解决这个问题,但在这里我找到了它,感谢 Alnitak。 在 URL 中使用 localhost 而不是主机名打开文件对我来说就是成功的秘诀。
Took me ages to solve this, but here I found it, thanks to Alnitak. Opening the file with localhost in the URL instead of the hostname was what did the trick for me.
您可以使用
ini_set('default_socket_timeout',2);
在打开 fopen $url 之前。 这实际上设置了默认的套接字连接超时而不响应。
Stream_set_timeout 设置通过 fopn 或套接字打开函数建立的流的超时。
尝试一下这个可能对你有帮助。
you can use
ini_set('default_socket_timeout',2);
before opening the fopen $url . This actually set the default socket connection timout without responding.
Stream_set_timeout sets time out on the stream that is established via fopn or socket opening functions.
Try this may be helpful for you.