Using wp_remote_get keeps pinging the API on every page load. Which increase the server resource.
Is it possible to cache the response of the API store it using Transients and use it for next 5 minutes instead of keep pinging everytime?
And After 5 Minutes it should send request again and rewrite the stored value.
Here is my code for API Request. How to do this? I'm new to this. Help me please
function display_api_response() {
$api_url = "https://randletter2020.herokuapp.com";
$response = wp_remote_get($api_url);
if ( 200 === wp_remote_retrieve_response_code($response) ) {
$body = wp_remote_retrieve_body($response);
if ( 'a' === $body ) {
echo 'A wins';
}else {
// Do something else.
}
}
}
add_action( 'init', 'display_api_response' );