Reputation: 33
When i use file_get_contents with a url (http://example.com or other), it will return false with different context, user_agent, etc:
file_get_contents(http://example.com):failed to open stream: operation failed
However when we used file_get_contents() for a local file, it works. But it shows the above error when we try to access any file through http.
I tried the same using cURL and got the following error:
cURL Error (7): couldn't connect to host
According to some solutions on stackoverflow, we changed this setting in php.ini file:
ini_set('allow_url_fopen', 1);
I have also used snoopy library after including snoopy class.php
require "Snoopy/Snoopy.class.php";
$snoopy = new $Snoopy;
fetchtext("http://example.com");
$text = $snoopy->results;
it is also giving an empty response.
If I cURL or wget the same URL using terminal, it works perfectly. But it doesn't work in the PHP code when the file is not local (through HTTP).
Server details:
Debian GNU/Linux 7.6 (wheezy)
Apache/2.2.22 (Debian)
PHP 5.4.45-0+deb7u7
Upvotes: 3
Views: 1421
Reputation: 575
In Debian, there will be a group aid_inet. Apache should be a member of that group to allow to access network.
Run the following command to get the apache user. In my machine its www-data.
cat /etc/passwd
Try this command to add apache user in aid_inet group.
usermod -a -G aid_inet www-data
[Note: if you have different apache user then use your own]
Now, run your php code again to test. It should work.
Upvotes: 4
Reputation: 1
Regarding from where you are running your php files ( apache or command line ), check that you have allow_url_fopen = On on both php.ini files : /etc/php5/cli/php.ini /etc/php5/apache2/php.ini
Upvotes: 0
Reputation: 15457
There is an error in your code, which could be the issue:
fetchtext("http://"google.com");
should be:
fetchtext("https://www.google.com");
(Google uses HTTPS, and always redirects to www
subdomain).
Have you checked that the URL you're attempting to query is valid (i.e. structure, escaping of special characters)? It would really help if you can give a real URL you're trying to call.
Upvotes: 0