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.

5
  • Which version of MAMP are you using? Commented Dec 7, 2011 at 22:20
  • Chances are google may reject requests via file_get_contents b/c a proper user-agent hasn't been defined, you may have to use curl; us3.php.net/curl Commented Dec 7, 2011 at 22:24
  • @Digital Precision Having the same problem with Curl. Has nothing to do with Google as any URL does not return anything. I bet it's my ISP. Commented Dec 7, 2011 at 22:37
  • @FFish: Refer to one of the other solutions, and ensure the allow_url_fopen is true. Commented Dec 7, 2011 at 22:38
  • @FFish - Can you try using file_get_contents with a numeric ip address of some website and see if that works? Commented Dec 7, 2011 at 23:06

7 Answers 7

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 
Sign up to request clarification or add additional context in comments.

2 Comments

@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!!!
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.
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.

5 Comments

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.
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??
Damn I just tried your last tip. Disable Little Snitch, my firewall. Now it works :-)) Thanks so much!
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 :-)
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
5

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

Comments

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

1 Comment

In /Applications/MAMP/conf/php5.3/php.ini allow_url_fopen = On Nothing :-(
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.

5 Comments

I'm not sure how this answers the question.
It tells that for me it was the issue of using https on localhost. It might work for others.
The question is about a connection from localhost to somewhere else, not from localhost to localhost.
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).
By localhost most people mean a not specially configured server which is most probably not reachable from the internet.
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.

Comments

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"); 

1 Comment

rawurlencode should be used when using querystrings ($_GET[]) in file_get_contents

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.