【问题标题】:Wordpress not displaying API dataWordpress 不显示 API 数据
【发布时间】:2016-08-18 04:32:32
【问题描述】:

我正在使用 Wordpress(小部件)调用 API。

但由于某种原因,它不允许我像这样显示嵌套对象:

private function get_request($username) {
        $url = wp_remote_get("https://api.github.com/users/essxiv/repos");
        $response = json_decode(stripslashes($url['body']));

        $nested_objs = $response[0]['id'];

        print_r($nested_objs);
}

我也试过print_r($response[0]['username']);

每次我尝试加载我的本地主机 Wordpress 时,它都会给我一个不同的 UI,没有管理标题,页面的边框是橙色而不是黑色..

我只是被难住了,真的需要显示这些嵌套对象的数据。

我做错了什么?如何打印数据?

【问题讨论】:

  • 想通了:应该是这样的:$name = $response[0]->{'name'};

标签: php wordpress api


【解决方案1】:

解决方案:

显然 PHP 的做法有点不同:

private function get_request($username) {
        $url = wp_remote_get("https://api.github.com/users/essxiv/repos");
        $response = json_decode(stripslashes($url['body']));

        $name = $response[0]->{'name'};
        $id = $response[0]->{'id'};
        $owner = $response[0]->{'owner'};

        print_r($name);
        print_r($id);
        print_r($owner);

}

如果你想找到一个嵌套对象键/值,它看起来像这样:

$owner = $response[0]->{'owner'}->{'login'};

【讨论】:

    【解决方案2】:

    使用 foreach 循环..

    function get_request($username='') {
                    $url = wp_remote_get("https://api.github.com/users/essxiv/repos");
    
                    $response = json_decode(stripslashes($url['body']));
                            foreach ($response as $key => $value) {
                                echo $value->id;
                                // $value->login;
                                // $value->avatar_url;
                            }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2019-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-19
      • 2022-12-09
      • 2021-02-20
      • 2020-06-25
      相关资源
      最近更新 更多