使用 file_get_contents

发布于 2024-12-18 07:17:55 字数 431 浏览 2 评论 0 原文

我使用 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 功能?抱歉,我英语不好,请允许我的问题。谢谢

I use file_get_content to leech rss.

I try use:

echo file_get_contents("http://vnexpress.net/gl/xa-hoi/2011/11/mot-so-bo-truong-the-hien-su-ne-tranh-trach-nhiem/");

Result is content of page

And I try use

echo file_get_content("http://hcm.24h.com.vn/ban-tre-cuoc-song/noi-doi-de-quyen-ru-chang-c64a418748.html"); 

Result is a blank page. why I can't get it? this page not allow file_get_contents function? sorry I bad english, please allow my question. thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

乖乖兔^ω^ 2024-12-25 07:17:55

尝试使用 file_get_contents 而不是 file_get_content

Try using file_get_contents instead of file_get_content.

深海里的那抹蓝 2024-12-25 07:17:55

你少了一个s。应该是

echo file_get_contents("http://hcm.24h.com.vn/ban-tre-cuoc-song/noi-doi-de-quyen-ru-chang-c64a418748.html");

You're missing a s. It should be

echo file_get_contents("http://hcm.24h.com.vn/ban-tre-cuoc-song/noi-doi-de-quyen-ru-chang-c64a418748.html");
国产ˉ祖宗 2024-12-25 07:17:55

首先,它是file_get_contents,而不是file_get_content
其次,它不适用于 http 链接。

http://vnexpress.net/gl/xa-hoi/2011/11/mot-so-bo-truong-the-hien-su-ne-tranh-trach-nhiem/

应该是这样的

/home/48564568/public_html/gl/xa-hoi/2011/11/mot-so-bo-truong-the-hien-su-ne-tranh-trach-nhiem/

取决于您的服务器。

要找到链接应该是什么,请尝试包含一个不存在的文件,如下所示

包括(somethingrandom.php);

并且您将收到一条错误通知,指出 /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.

http://vnexpress.net/gl/xa-hoi/2011/11/mot-so-bo-truong-the-hien-su-ne-tranh-trach-nhiem/

should be something like

/home/48564568/public_html/gl/xa-hoi/2011/11/mot-so-bo-truong-the-hien-su-ne-tranh-trach-nhiem/

depending on your server.

To find what the link should be, try to include a nonexisting file like so

include(somethingrandom.php);

and you will receive an error notice that /the/path/you/need/somethingrandom.php doesn't exist.

琴流音 2024-12-25 07:17:55

PHP 文档明确指出 file_get_contents 支持远程文件,但您需要启用 allow_url_fopen 配置选项。如果未设置 user_agent 指令,许多网站(根据我的经验)会避免提供内容。您还应该尝试将此值设置为有效的用户代理字符串。

来自 https://www.php.net/manual/en /function.file-get-contents.php

示例#1 获取并输出网站主页的源码

<前><代码>

From https://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen

这是配置指令的简短说明。

allow_url_fopen布尔值

此选项启用 URL 感知的 fopen
允许访问 URL 对象(如文件)的包装器。默认包装器
提供使用 ftp 或 http 访问远程文件
协议,某些扩展(例如 zlib)可能会注册额外的包装器。

可能有很多事情(超出了这个答案的范围)可能会阻止您提取文件,但我多次发现只需将 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

Example #1 Get and output the source of the homepage of a website

<?php
$homepage = file_get_contents('http://www.example.com/');
echo $homepage;
?>

From https://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen

Here's a short explanation of the configuration directives.

allow_url_fopen boolean

This option enables the URL-aware fopen
wrappers that enable accessing URL object like files. Default wrappers
are provided for the access of remote files using the ftp or http
protocol, some extensions like zlib may register additional wrappers.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文