【发布时间】:2021-04-12 02:06:44
【问题描述】:
我正在使用 wp_remote_get 方法进行 API 调用,以从外部源(不是 WP 站点)获取数据。 API 调用正在工作,我得到了我想要的响应,这只是一个数字。现在我想将此数字(响应)存储为 wp_postmeta 中的 meta_value,并带有以下 meta_key(donation_amount)和特定的 post_id。数据库表已经存在。到目前为止,我所拥有的是 API 调用和响应。我已经尝试过 wp_update_post 但也许我做错了什么或者可能不是正确的方法。任何帮助将不胜感激。
$url = 'https://developer.mozilla.org/en-US/docs/Web/API/URL_API'; // Not the real url
$response = wp_remote_get( $url );
if( is_wp_error( $response ) ) {
return false;
} else {
$api_response = json_decode( wp_remote_retrieve_body( $response ), true );
}
echo $api_response ['theNumber'];
【问题讨论】: