Page 1 of 1

file_get_contents does not work when trying to read webpages

Posted: Tue Mar 06, 2007 5:10 am
by guarriman
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi.

Working on Linux, I created this script:

Code: Select all

$url = "http://www.mydomain.com";
$html = @file_get_contents($url, "r") or die("Can't open URL");
echo $html;
This script:
- works ok on my PC box (PHP 4.4.5) with 'http://www.google.com'
- works ok on my PC box (PHP 4.4.5) with 'http://www.mydomain.com'
- works ok on my server (PHP 4.4.2) with 'http://www.google.com'
- DOES NOT work on my server (PHP 4.4.2) with 'http://www.mydomain.com'

Does anybody have any tip to find out why I can't read 'http://www.mydomain.com' on my server? Must I modify something on my 'mydomain.com' server (I manage it)?

Thank you very much.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Mar 06, 2007 5:17 am
by volka
remove the @ and let the script start with

Code: Select all

error_reporting(E_ALL); ini_set('display_errors', true);

Posted: Tue Mar 06, 2007 6:19 am
by aaronhall
It's likely that your server is blocking outbound request. A "permission denied" error would indicate that.

Posted: Tue Mar 06, 2007 8:53 am
by feyd
The second parameter accepted by file_get_contents() is not a string.

Posted: Wed Mar 07, 2007 4:21 am
by guarriman
Hi.

Thank you very much for your answers. I get this errors when 'error_reporting':

Code: Select all

Warning: file_get_contents(http://www.mydomain.com): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in /home/project/httpdocs/www/test.php on line 4
Can't open remote URL!
In addition this 403 error message is given by my own server, since http://www.mydomain.com is hosted within the same machine as my project. So I guess I must reconfigure my Apache server to allow serve webpages to itself.

Posted: Wed Mar 07, 2007 12:15 pm
by feyd
If the files are accessible via the file system, it would reduce bandwidth requirements.