I'm trying to figure out how to best solve an issue I have with 3rd party API's (OG, Foursquare, Yelp, etc.). I use the transient API to call and store the various data in order to:
- Not exceed any API limits
- Increase load speed
However, the issue arises when a new API call errors out for whichever reason; either there was an issue with the connection, or the API itself was down (hello foursquare). This leads to a scenario where you have no new data and the old data timed out (which is essentially what triggered a new transient to generate). How do you deal with such situations?
The solution I have in mind is to create a static option within the refresh function that stores a successful response or brings up the last successful response on an error, for example :
<?php
function refresh_api_data() {
... perform API call ...
if ( $response->status == 'error' ) {
$response = get_option( 'fallback_data' );
} else {
update_option( 'fallback_data', $response );
}
return $response;
}
?>
Does this make sense, or does anyone have a more elegant solution in mind?
Thank you!