使用 file_get_contents
我使用 file_get_content 来获取 rss。
我尝试使用:
echo file_get_contents("http://vnexpress.net/gl/xa-hoi/2011/11/mot-so-bo-truong-the-hien-su-ne-tranh-trach-nhiem/");
结果是页面内容
我尝试使用
echo file_get_content("http://hcm.24h.com.vn/ban-tre-cuoc-song/noi-doi-de-quyen-ru-chang-c64a418748.html");
结果是空白页面。为什么我拿不到?此页面不允许 file_get_contents 功能?抱歉,我英语不好,请允许我的问题。谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用
file_get_contents
而不是file_get_content
。Try using
file_get_contents
instead offile_get_content
.你少了一个s。应该是
You're missing a s. It should be
首先,它是file_get_contents,而不是file_get_content。
其次,它不适用于 http 链接。
应该是这样的
取决于您的服务器。
要找到链接应该是什么,请尝试包含一个不存在的文件,如下所示
并且您将收到一条错误通知,指出 /the/path/you/need/somethingrandom.php 不存在。
First of all it's file_get_contents, not file_get_content.
Secondly it's not going to work with a http link.
should be something like
depending on your server.
To find what the link should be, try to include a nonexisting file like so
and you will receive an error notice that /the/path/you/need/somethingrandom.php doesn't exist.
PHP 文档明确指出 file_get_contents 支持远程文件,但您需要启用 allow_url_fopen 配置选项。如果未设置 user_agent 指令,许多网站(根据我的经验)会避免提供内容。您还应该尝试将此值设置为有效的用户代理字符串。
来自 https://www.php.net/manual/en /function.file-get-contents.php
From https://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
可能有很多事情(超出了这个答案的范围)可能会阻止您提取文件,但我多次发现只需将 user_agent 指令设置为有效字符串即可解决问题。
PHP documentation clearly states that file_get_contents supports remote files, but you need to enable the allow_url_fopen configuration option. Many sites (in my experience) avoid delivering content if the user_agent directive is not set. You should also try to set this value to a valid user agent string.
From https://www.php.net/manual/en/function.file-get-contents.php
From https://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
There could be a lot of things (outside of the scope of this answer) that could be preventing you from pulling the file, but I have found multiple times that just setting the user_agent directive to a valid string will do the trick.