无法在 Wordpress 中使用 REST API 获得响应
Can't get response using REST API in Wordpress
更新:修复在答案中!
请帮忙!
我想使用 RapidApi 中的 Instgram api。这就是 RapidAPI 显示为 PHP 片段的内容:
<?php
$request = new HttpRequest();
$request->setUrl('https://instagram47.p.rapidapi.com/public_user_posts');
$request->setMethod(HTTP_METH_GET);
$request->setQueryData([
'userid' => '1718924098'
]);
$request->setHeaders([
'x-rapidapi-key' => ' ***************** ',
'x-rapidapi-host' => 'instagram47.p.rapidapi.com'
]);
try {
$response = $request->send();
echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}
这是我的代码在 functions.php 中的样子:
function get_instaPosts(){
$request = wp_remote_retrieve_body( wp_remote_get( 'https://instagram47.p.rapidapi.com/user_posts', array(
'headers' => array(
"content-type" => "application/json",
'userid' => '1718924098',
'x-rapidapi-key' => 'INSTA_APIKEY',
'x-rapidapi-host' => 'instagram47.p.rapidapi.com'
),
'body' => array()
)
));
$response_code = wp_remote_retrieve_response_code($response);
$body = wp_remote_retrieve_body($response);
if( 401 === $response_code) {
echo "<pre>";
print_r("Unauthorised access");
echo "</pre>";
}
if( 200 !== $response_code) {
echo "<pre>";
print_r("Error in pinging API");
echo "</pre>";
}
if( 200 === $response_code) {
echo "<pre>";
print_r($body);
echo "</pre>";
}
}
但它始终打印在 if (200!==...)
语句中定义的 "Error in pinging API"
。打印 $response 在两者之间:
[body] => {"message":"Missing required parameters"}
最后,因为他们给了你一个秘密的 API 密钥,像这样将实际密钥保存为 wp-config 中的变量并稍后仅使用 INSTA_APIKEY 引用它是一个好习惯吗没能做到 public:
/** API key for RapidApi Insta */
define( 'INSTA_APIKEY', '***************' );
非常感谢你们,你们不知道这个平台对我有多大帮助。 :D
修复:
在 functions.php:
function get_instaPosts(){
$userid = ' ** insta userid here ** ';
$first = '2';
$after = ' ** the after code here ** ';
$response = wp_remote_get( "https://instagram40.p.rapidapi.com/account-medias?userid=$userid&first=$first&after=$after", array(
'headers' => array(
'x-rapidapi-key' => ' ** secret key ** ',
'x-rapidapi-host' => 'instagram40.p.rapidapi.com'
)
)
);
$response_code = wp_remote_retrieve_response_code($response);
$body = wp_remote_retrieve_body($response);
$decode = json_decode( $body, true );
更新:修复在答案中!
请帮忙!
我想使用 RapidApi 中的 Instgram api。这就是 RapidAPI 显示为 PHP 片段的内容:
<?php
$request = new HttpRequest();
$request->setUrl('https://instagram47.p.rapidapi.com/public_user_posts');
$request->setMethod(HTTP_METH_GET);
$request->setQueryData([
'userid' => '1718924098'
]);
$request->setHeaders([
'x-rapidapi-key' => ' ***************** ',
'x-rapidapi-host' => 'instagram47.p.rapidapi.com'
]);
try {
$response = $request->send();
echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}
这是我的代码在 functions.php 中的样子:
function get_instaPosts(){
$request = wp_remote_retrieve_body( wp_remote_get( 'https://instagram47.p.rapidapi.com/user_posts', array(
'headers' => array(
"content-type" => "application/json",
'userid' => '1718924098',
'x-rapidapi-key' => 'INSTA_APIKEY',
'x-rapidapi-host' => 'instagram47.p.rapidapi.com'
),
'body' => array()
)
));
$response_code = wp_remote_retrieve_response_code($response);
$body = wp_remote_retrieve_body($response);
if( 401 === $response_code) {
echo "<pre>";
print_r("Unauthorised access");
echo "</pre>";
}
if( 200 !== $response_code) {
echo "<pre>";
print_r("Error in pinging API");
echo "</pre>";
}
if( 200 === $response_code) {
echo "<pre>";
print_r($body);
echo "</pre>";
}
}
但它始终打印在 if (200!==...)
语句中定义的 "Error in pinging API"
。打印 $response 在两者之间:
[body] => {"message":"Missing required parameters"}
最后,因为他们给了你一个秘密的 API 密钥,像这样将实际密钥保存为 wp-config 中的变量并稍后仅使用 INSTA_APIKEY 引用它是一个好习惯吗没能做到 public:
/** API key for RapidApi Insta */
define( 'INSTA_APIKEY', '***************' );
非常感谢你们,你们不知道这个平台对我有多大帮助。 :D
修复: 在 functions.php:
function get_instaPosts(){
$userid = ' ** insta userid here ** ';
$first = '2';
$after = ' ** the after code here ** ';
$response = wp_remote_get( "https://instagram40.p.rapidapi.com/account-medias?userid=$userid&first=$first&after=$after", array(
'headers' => array(
'x-rapidapi-key' => ' ** secret key ** ',
'x-rapidapi-host' => 'instagram40.p.rapidapi.com'
)
)
);
$response_code = wp_remote_retrieve_response_code($response);
$body = wp_remote_retrieve_body($response);
$decode = json_decode( $body, true );