11

I am working on my website from localhost (http://172.16.65.1/) a MAMP server on OSX.
I want to load some JSON from Google and some simple tests show me I have a problem here..

echo file_get_contents("http://www.google.com"); // FAILS
// PHP log: [07-Dec-2011 23:09:21] PHP Warning:  file_get_contents(http://www.google.com) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: Host is down in /Applications/MAMP/htdocs/-tests/get-json.php on line 3
echo file_get_contents("http://www.yahoo.com"); // FAILS

// echo file_get_contents("http://localhost"); // WORKS
// echo file_get_contents("http://172.16.65.1/"); // WORKS - My MAMP server

What can I do about this? It works fine on my host providers server.

FFish
  • 10,964
  • 34
  • 95
  • 136

7 Answers7

14

You need to also check in PHP.ini file

extension = php_openssl.dll

is enable or not, if not then just enable that by removing ; sign

allow_url_fopen = on
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Niranjan
  • 149
  • 1
  • 2
  • @Wolfeh Yeah. This answer should be higher! And rightly, it is higher! Because it actually solved the empty response of ***cURL*** which I have been battling with for hours, even on SO!!! – Damilola Olowookere Mar 30 '17 at 16:12
  • I have those and it still doesn't work for https urls. It does work if I run the php on a real server but not from localhost. – Curtis Dec 24 '19 at 04:53
7

From the documentation for file_get_contents:

A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.

Check in your php.ini so allow_url_fopen is set to on.

EDIT:

I didn't noticed that you actually could use file_get_contents locally, so now I'm thinking that this could have something to do with your firewall settings.

Also, try to set the user_agent in your php.ini if not already done.

Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
  • And you are sure that it's the correct `php.ini` file you're looking at? If not try running a `phpInfo()` to see wich `php.ini` gets loaded. – Cyclonecode Dec 07 '11 at 22:49
  • yeah, it's the right php.ini file and was allready on. I also tried with my mobile internet connection (other ISP) no go either.. "failed to open stream: Host is down" WHY Host is down?? – FFish Dec 07 '11 at 23:08
  • 2
    Damn I just tried your last tip. Disable Little Snitch, my firewall. Now it works :-)) Thanks so much! – FFish Dec 07 '11 at 23:09
  • 1
    Ok just as a note I had a rule set for `httpd` to deny any connection. Dunno why I did that because I don't even know what `httpd` actually is. Probably the popups from my Firewall where bugging me and I killed it. btw also Curl works now, Zuper :-) – FFish Dec 07 '11 at 23:15
  • Could you be more specific about the `httpd` config setting you had set to 'deny' @FFish? What was the path and the specific setting name: I am looking to check I don't have the same – Scott Anderson Apr 15 '19 at 12:21
4

Try this function in place of file_get_contents():

<?php

function curl_get_contents($url)
{
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

It can be used just like file_get_contents(), but uses cURL.

Install cURL on Ubuntu (or other unix-like operating system with aptitude):

sudo apt-get install php5-curl
sudo /etc/init.d/apache2 restart

See also cURL

Adelmar
  • 2,073
  • 2
  • 20
  • 20
3

This may be a setting in your php.ini file. There is a setting for allow_url_fopen which enables/disables the ability to open remote files from php. For security reasons this is usually defaulted to disabled. You can enable it in your php.ini by adding the following line:

allow_url_fopen = 1

Again, be aware of the security concerns when using this feature.

http://php.net/manual/en/filesystem.configuration.php

dchrastil
  • 582
  • 3
  • 5
2

For me the problem was that file_get_contents didn't work on https://domain.test.loc (domain that resolves to localhost), but worked on http://domain.test.loc. Maybe it doesn't like the self-signed certificate. I do have allow_url_fopen set to ON and extension = php_openssl.dll.

Lyubomir
  • 140
  • 5
  • 13
  • 1
    I'm not sure how this answers the question. – csabinho Nov 06 '19 at 18:39
  • 1
    It tells that for me it was the issue of using https on localhost. It might work for others. – Lyubomir Nov 06 '19 at 18:41
  • The question is about a connection from localhost to somewhere else, not from localhost to localhost. – csabinho Nov 06 '19 at 18:42
  • I'm not trying to defend, but there is something wrong about this title: "PHP file_get_contents does not work on localhost" - what exactly is not localhost on this world. Basically code works on localhost. If you're using a development machine or server the code runs on it (localhost). – Lyubomir Nov 06 '19 at 18:48
  • 1
    By `localhost` most people mean a not specially configured server which is most probably not reachable from the internet. – csabinho Nov 06 '19 at 19:27
1

In the second one you're trying to open a file named localhost in the current folder, which doesn't exist and hence throws an error. Use http://localhost instead. And to make that work, you're have to set allow_furl_open.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
0

I was using file_get_contents this way: file_get_contents("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=".$screenname."&count=5");

And that was not working, so I changed https to http and after that it is working well.

file_get_contents("http://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=".$screenname."&count=5");
Fabio Antunes
  • 22,251
  • 15
  • 81
  • 96
karambir
  • 101
  • 1
  • 1