为什么我不能在没有偏移错误的情况下从以下 laravel-wp-api JSON 响应中提取信息?

Why can't I extract information from the following laravel-wp-api JSON response without an offset error?

我正在使用 laravel-wp-api to pull in some Wordpress data from a Wordpress page 但难以从以下 JSON 响应中提取数据:

{"results":[{"id":8,"date":"2017-01-31T07:08:21","date_gmt":"2017-01-31T07:08:21","guid":{"rendered":"http:\/\/idybrand.com\/wordpress\/?p=8"},"modified":"2017-01-31T07:08:21","modified_gmt":"2017-01-31T07:08:21","slug":"february-is-just-around-the-corner","type":"post","link":"http:\/\/idybrand.com\/wordpress\/2017\/01\/31\/february-is-just-around-the-corner\/","title":{"rendered":"February is just around the corner"},"content":{"rendered":"\u003Cp\u003EAppreciate this wonderful month\u003C\/p\u003E\n","protected":false},"excerpt":{"rendered":"\u003Cp\u003EAppreciate this wonderful month\u003C\/p\u003E\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/idybrand.com\/wordpress\/wp-json\/wp\/v2\/posts\/8"}],"collection":[{"href":"http:\/\/idybrand.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/idybrand.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/idybrand.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/idybrand.com\/wordpress\/wp-json\/wp\/v2\/comments?post=8"}],"version-history":[{"href":"http:\/\/idybrand.com\/wordpress\/wp-json\/wp\/v2\/posts\/8\/revisions"}],"wp:attachment":[{"href":"http:\/\/idybrand.com\/wordpress\/wp-json\/wp\/v2\/media?parent=8"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/idybrand.com\/wordpress\/wp-json\/wp\/v2\/categories?post=8"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/idybrand.com\/wordpress\/wp-json\/wp\/v2\/tags?post=8"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}],"total":"1","pages":"1"}

尝试获取 results(现在只有一个 post)会出现 Illegal string offset 'results' 错误。这是我的代码:

$response = json_encode(WpApi::posts(),true);
echo $response;

$posts = $response['results'];
echo $posts; //Illegal string offset 'results' error

$posts = $response->results; //Of course this won't work either
echo $posts; //Trying to get property of non-object error

第一个 echo 打印上面粘贴的响应。如何提取 Wordpress post 数据,例如 titlecontent

API Reference for posts

任何帮助都会很棒。

编辑: Mayank 建议使用 var_dump(WpApi::posts()); 并且 returns:

array(3) { ["results"]=> array(1) { [0]=> array(23) { ["id"]=> int(8) ["date"]=> string(19) "2017............... 

所以它已经是一个数组了。但是如何提取数据,例如titlecontent

更改此行:

$response = json_encode(WpApi::posts(),true);

$response = json_decode(WpApi::posts(),true);
// 2nd parameter true means it returns an array

$posts = $response['results'];

实际回答我问的问题:

$arr = WpApi::posts();
echo $arr['results'][0]['title']['rendered']; //Title
echo $arr['results'][0]['content']['rendered']; //Content

你需要做的

foreach ($posts['results'] as $post) {
 echo $post['title'].'<br>'.$post['content'].'<br>';
}