file_get_contents()

driveflexfuel

New Member
Messages
159
Reaction score
0
Points
0
I am using file_get_contents('http://www.site.com'); to connect to another one of my sites to retrieve information. If for some reason my site is down i need it to prompt the script.

Example

$info = file_get_contents('http://www.site.com/updates/index.php');

If there is no error

echo $info

else

Echo "unable to check updates at this time.";
 

gomarc

Member
Messages
516
Reaction score
18
Points
18
On failure, file_get_contents() will return FALSE.

You can do something like this:

PHP:
$info = file_get_contents('http://www.site.com/updates/index.php') 
  or exit("</br> Unable to check updates at this time.");

//there is no error, continue

echo $info;
 

driveflexfuel

New Member
Messages
159
Reaction score
0
Points
0
I still receive the following errors on the screen that i was hoping to remove with the error code. I did what you showed and it does display Unable to check updates at this time but does not remove theses errors



Warning
: file_get_contents() [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/e85and/public_html/about.php on line 7

Warning: file_get_contents(http://www.site.com/updates/index.php) [function.file-get-contents]: failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/e85and/public_html/about.php on line 7


 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
You can prevent the display of non-fatal errors in a webpage with ini_set. Try
Code:
ini_set('display_errors', 0);
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
You can also suppress errors for one line using @.

If you are trying to file_get_contents() from a site outside of your own, consider this (from php.net):

Note: If you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode().

:)
 
Top