Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP file_get_contents does not work on localhost

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.

like image 298
FFish Avatar asked Dec 07 '11 22:12

FFish


2 Answers

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
like image 68
Niranjan Avatar answered Oct 05 '22 20:10

Niranjan


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.

like image 35
Lyubomir Avatar answered Oct 05 '22 21:10

Lyubomir