Error checking for file_get_contents

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
realtime158
Forum Newbie
Posts: 14
Joined: Mon Nov 20, 2006 9:09 pm

Error checking for file_get_contents

Post by realtime158 »

I was wondering if there is a way so that if file_get_contents has a problem reading a URL page that it will not crash the php script. So like on error just continue or skip. I am using PHP 4.4.4

Thanks...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Last I checked, file_get_contents() outputs a warning upon encountering problems. This does not stop execution.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Per the manual you can set the timeout value..

Code: Select all

ini_set('default_socket_timeout',    120);
But I'd recommend you just use curl instead.
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 »

Check with file_exists() prior to calling file_get_contents()
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Note: file_exists() only supports URLs in PHP 5+.

Edit: However, user comments on the page supply potential url_exists() functions that work similarly. Some do require PHP 5, so be careful.
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 »

eek, didn't know that, thanks
Post Reply