【发布时间】:2019-07-19 00:32:22
【问题描述】:
我正在尝试从此 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;
【问题讨论】:
-
文档中的示例使用 POST 请求,尝试更改为
wp_remote_post()方法而不是wp_remote_get() -
这是我的理解:POST 用于将数据添加到外部 API 的数据库中,但在我的情况下,我想从 API 中检索数据并且我认为我应该使用 GET?我也尝试使用 POST 看看会发生什么,结果仍然相同。还是谢谢
标签: php wordpress api rest rapidapi