3

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

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the valuable suggestion. It works well and due to it and with your support my issue is resolved now. Thank you again
0

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.

6 Comments

That was meant to be an example. We tried several (non-https) domains. Still didn't work.
Can you give a real URL to test with?
That's probably the issue then. Is the URL you're trying to access accessible via other means (i.e. browser, command line curl, etc)? And is the server you are running the code on able to access your local network.
If you would read the entire question you'd find that we already tried accessing it through command line curl and wget. It works that way. But not in the php code.
Your statement doesn't make sense: 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). Does that mean the curl / wget didn't use HTTP?
|
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.