views:

159

answers:

3

I use following PHP function:

file_get_contents('http://example.com');

Whenever I do this on a certain server, the result is empty. When I do it anywhere else, the result is whatever the page's content may be. When I however, on the server where the result is empty, use the function locally - without accessing an external URL (file_get_contents('../simple/internal/path.html');), it does work.

Now, I am pretty sure it has something to do with a certain php.ini configuration. What I am however not sure about is, which one. Please help.

+4  A: 

The setting you are looking for is allow_url_fopen.

You have two ways of getting around it without changing php.ini, one of them is to use fsockopen(), and the other is to use cURL.

I recommend using cURL over file_get_contents() anyways, since it was built for this.

Aillyn
This is not entirely correct. You can use cURL with `file_get_contents()` with the configure option `--with-curlwrappers`.
Artefacto
@Artefacto I don't understand. Please elaborate.
Aillyn
If you compile the curl extension with `--with-curlwrappers`, curl will be used to make a HTTP request whenever you do `file_get_contents("http://example.com/stuff")`. My point was that curl and `file_get_contents` are not orthogonal, it's not "use one or the other".
Artefacto
@Artefacto I didn't know that. Thanks!
Aillyn
+1  A: 

The is related to the ini configuration setting allow_url_fopen.

You should be aware that enable that option may make some bugs in your code exploitable.

For instance, this failure to validate input may turn into a full-fledged remote code execution vulnerability:

copy($_GET["file"], "."); 
Artefacto
A: 

Complementing Aillyn answer, you could use a function like the one below instead of file_get_contents:

      function get_content($URL){
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_URL, $URL);
          $data = curl_exec($ch);
          curl_close($ch);
          return $data;
      }


 echo get_content('http://example.com');
Pablo