file_get_contents does not work when trying to read webpages

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
guarriman
Forum Commoner
Posts: 44
Joined: Thu Nov 03, 2005 4:11 am

file_get_contents does not work when trying to read webpages

Post 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]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

remove the @ and let the script start with

Code: Select all

error_reporting(E_ALL); ini_set('display_errors', true);
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

It's likely that your server is blocking outbound request. A "permission denied" error would indicate that.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The second parameter accepted by file_get_contents() is not a string.
guarriman
Forum Commoner
Posts: 44
Joined: Thu Nov 03, 2005 4:11 am

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If the files are accessible via the file system, it would reduce bandwidth requirements.
Post Reply