i have a php script like this:
<?php
$likes = 'https://graph.facebook.com/google';
$fb = file_get_contents($likes);
$fb_array=json_decode($fb,true);
$all_likes = $fb_array['likes'];
$english_format = number_format($all_likes);
?>
what happens sometimes is that the url is failing and i get something like this:
Warning: file_get_contents(https://graph.facebook.com/google) [function.file-get-contents]: failed to open stream: HTTP request failed! in /var/www/html/google/index.php on line 774
I was wondering if there is a way for the code to degrade gracefully, because is bringing my entire website down.
I was thinking if there is a php or curl alternative to handle that error.
any ideas?
Thanks
edit:
i could so this:
<?php
$likes = 'https://graph.facebook.com/google';
if(!@file_get_contents($likes)){
$english_format = 123;
} else {
$fb = file_get_contents($likes);
$fb_array=json_decode($fb,true);
$all_likes = $fb_array['likes'];
$english_format = number_format($all_likes);
}
?>
but it still slows down my website
You can handle HTTP errors with
file_get_contentsby using a stream context:Additionally,
display_errorsshould beOffon production environments.