使用wp\\u remote\\u get会在每次页面加载时不断ping API。这增加了服务器资源。
是否可以缓存API的响应,使用瞬态存储它,并在接下来的5分钟内使用它,而不是每次都保持ping?
5分钟后,它应该再次发送请求并重写存储的值。
这是我的API请求代码。如何做到这一点?我是新手。请帮帮我
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\' );