使用file_get_contents

时间:2011-11-25 14:55:59

标签: php 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功能?抱歉,我的英文不好,请允许我的问题。感谢

5 个答案:

答案 0 :(得分:4)

尝试使用file_get_contents代替file_get_content

答案 1 :(得分:3)

你错过了一个。它应该是

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

答案 2 :(得分:1)

首先,它是 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/

应该是

  

/家庭/ 48564568 /的public_html / GL / XA恺/ 2011/11 / MOT-所以波-TRUONG最HIEN-SU-NE-tranh-气管-nhiem /

取决于您的服务器。

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

  

包括(somethingrandom.php);

您将收到 /the/path/you/need/somethingrandom.php 不存在的错误通知。

答案 3 :(得分:0)

首先是file_get_contents,而不是file_get_content。其次,它不适用于http链接。 http://truyen-cuoi.net/

答案 4 :(得分:0)

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

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

  

示例1:获取并输出网站首页的来源

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

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

  

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

     

allow_url_fopen 布尔值

     

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

可能有很多事情(超出了此答案的范围)可能会阻止您提取文件,但是我发现很多次仅将user_agent指令设置为有效字符串即可解决问题。

相关问题