Php wp_remote_get:获取接收到的数据
我正在尝试使用此api获取特定数据(用于Wordpress博客): 我对数组感到迷茫,无法隔离特定数据:例如,如果我想要“chess_daily”的“Last”“rating”。下面是我使用的代码,但我不知道如何定位数据Php wp_remote_get:获取接收到的数据,php,wordpress,api,Php,Wordpress,Api,我正在尝试使用此api获取特定数据(用于Wordpress博客): 我对数组感到迷茫,无法隔离特定数据:例如,如果我想要“chess_daily”的“Last”“rating”。下面是我使用的代码,但我不知道如何定位数据 <?php $request = wp_remote_get( 'https://api.chess.com/pub/player/erik/stats' ); if( is_wp_error( $request ) ) { return false; } $body
<?php $request = wp_remote_get( 'https://api.chess.com/pub/player/erik/stats' );
if( is_wp_error( $request ) ) {
return false;
}
$body = wp_remote_retrieve_body( $request);
$test = json_decode( $body,true );
print_r ($test);
?>
PHP7添加了空合并运算符,这是从数组中获取数据的一种好方法。对于您的示例,您可以运行:
$last\u chess\u daily=$test['chess\u daily']['last']??无效的
返回:
数组(
“评级”=>1604,
“日期”=>1616714557,
“rd”=>51,
)