I'm working on a WordPress plugin that returns the follower count for a specific Forrest user.
Would like the function to gracefully return 0 if there was an error communicating with the API server or any other problems.
Here is the function:
/**
* Get Forrst followers.
*
* @param string $forrstID The username of the Forrst member
* @return int. Number of Forrst Followers
*/
function ass_get_forrst($forrstID) {
$json = wp_remote_get("http://forrst.com/api/v2/users/info?username=".$forrstID);
if(is_wp_error($json))
return false;
$forrstData = json_decode($json['body'], true);
return intval($forrstData['resp']['followers']);
}
I have a block in the function to return false if there was an error however it seems that this section must be skipped as sometime I still end up with "Fatal Errors" IE Maximum execution time exceeded.
Is there a better way I could rewrite this function to return "0" if there was an error. Maybe a Try/Catch block?
Do I have the if(is_wp_error($json)) return false; in the wrong section of the function?