【问题标题】:Can't get response using REST API in Wordpress无法在 Wordpress 中使用 REST API 获得响应
【发布时间】:2021-06-12 09:54:38
【问题描述】:

更新:答案就是修复!

请帮忙!

我想使用 RapidApi 的 Instgram api。这就是 RapidAPI 显示为 PHP sn-p 的内容:

<?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 引用它以不公开它是一种好习惯吗:

/** API key for RapidApi Insta */
define( 'INSTA_APIKEY', '***************' );

非常感谢你们不知道这个平台对我有多大帮助。 :D

【问题讨论】:

  • 与其更改问题,不如恢复您的原始问题并添加解决方案(问题的当前内容)作为答案。
  • @NigelRen 我是堆栈交换的新手。如果有人想测试,我已经用相关 API 的链接更新了我的 OP。将来会记住您的建议。 :)
  • 我建议您按照@NigelRen 的指导解决当前问题以及将来的问题。应用这些准则可确保为可能遇到相同问题的未来访问者提供高质量的内容。
  • @RagingVids Nigel 所说的是编辑您的问题以将其恢复为无效的内容,然后发布工作代码作为答案。不要用工作代码更新问题,因为它会显示为没有答案,并且将来不会帮助任何人。

标签: php wordpress api rapidapi


【解决方案1】:

修复: 在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 );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-11
    • 2014-06-06
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    相关资源
    最近更新 更多