-2

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

ajreal
  • 46,720
  • 11
  • 89
  • 119
vinanghinguyen
  • 1,233
  • 4
  • 11
  • 17
  • this is likely a network issue, or the hosting site blocking your access (from your network or ip) – ajreal Nov 25 '11 at 14:58
  • This is not an RSS feed. The RSS feed is located at http://www.24h.com.vn/upload/rss/bantrecuocsong.rss. See if http://stackoverflow.com/questions/5975213/how-can-i-download-using-php-a-xml-file-redirected-in-some-weird-way/5975340#5975340 solves your problem. – Gordon Nov 25 '11 at 17:24

4 Answers4

4

Try using file_get_contents instead of file_get_content.

Nikola K.
  • 7,093
  • 13
  • 31
  • 39
Pr0no
  • 3,910
  • 21
  • 74
  • 121
3

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");
Jan Dragsbaek
  • 8,078
  • 2
  • 26
  • 46
1

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.

  • please describe why file_get_contents wont work with http link – NullPoiиteя Jul 27 '12 at 17:15
  • It's a security measure. If anyone could include a remote php file one could include it on their website and have your server execute the PHP code.You can get around this security measure by setting allow_url_include to ON on php.ini, but I wouldn't recommend it because of the formerly addressed security leak. –  Jul 28 '12 at 09:26
1

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.

Community
  • 1
  • 1
Francisco Félix
  • 2,413
  • 13
  • 16