我正在尝试从此 API 获取数据:
https://rapidapi.com/apilayernet/api/rest-countries-v1?
endpoint=53aa5a08e4b0a705fcc323a6
我设法使用 wp_remote_get() 发出请求,但除了错误之外,我一直没有显示任何结果:
The site is experiencing technical difficulties.
我只是指出我已经使用 Composer 在包含请求的 XAMPP 正确文件夹中设置 Composer.json 文件:
{
"require-dev": {
"mashape/unirest-php": "3.*"
}
}
在我的代码中,我包含了 API 密钥的参数,如下所示,但由于某种原因不起作用:
$request = wp_remote_get( 'https://restcountries-v1.p.rapidapi.com/all',
array(
"X-RapidAPI-Host" => "restcountries-v1.p.rapidapi.com",
"X-RapidAPI-Key" => "7fc872eb0bmsh1baf0c288235a1ep114aecjsn18f888f020c0"
) );
if( is_wp_error( $request ) ) {
return false; // Bail early
}
$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body );
echo $data;