$body = get_transient( 'my_api_response_value' );
if ( false === $body )
{
$url="https://api.myapi.com/chart?ticker=" . $ticker . '';
$response = wp_remote_get($url);
if (200 !== wp_remote_retrieve_response_code($response)) {
return;
}
$body = wp_remote_retrieve_body($response);
set_transient( 'my_api_response_value', $body, 5*MINUTE_IN_SECONDS );
}
$formatted_json = json_decode($body, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
We submit a request at the beginning because the transient doesn’t yet exist, and then we save the $body as a transitory value.
More info about the transient go to this Link : https://developer.wordpress.org/reference/functions/set_transient/