UPDATE:修复就是答案!
请帮忙!
我想使用RapidApi的InstgramApi。这就是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.hp:中的样子
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"
。打印$响应将在两者之间给出:
[body] => {"message":"Missing required parameters"}
最后,由于它们为您提供了一个秘密的API密钥,因此将实际密钥保存为wp-config中的一个变量并在稍后仅使用INSTA_APIKEY引用它以不公开它是一种好的做法吗:
/** API key for RapidApi Insta */
define( 'INSTA_APIKEY', '***************' );
非常感谢,伙计们,你们不知道这个平台对我有多大帮助。:D
FIX:在functions.hp:中
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 );