简体   繁体   中英

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.

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

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.

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

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

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM