无法从Facebook Graph API访问PHP中的对象变量

时间:2017-06-27 10:15:40

标签: php facebook-graph-api

我向Facebook Graph API发出请求,要求从Facebook帐户返回帖子。

我只是想尝试访问PHP中返回的对象的数据实例,但我尝试过的所有内容都返回 NULL

示例回复

{
   "data": [
      {
         "id": " 111111111111111111100000",
         "message": "Coming soon #PERFECTFIT 05.07.17 \nRegister to be one of the first to find out what it is here ⬇️\nhttp://www.bathrugby.com/the-club/supporters/perfect-fit-register/",
         "created_time": "2017-06-26T17:39:20+0000",
         "link": "http://www.bathrugby.com/the-club/supporters/perfect-fit-register/",
         "full_picture": "https://scontent.xx.fbcdn.net/v/t39.2147-6/19284954_1592534984092755_4946207882807869440_n.jpg?oh=56cc96435f423cec31962966b6f689c2&oe=59DB08B6"
      }
   ]
}

我想获取数据提供的对象数组,这样我就可以在MVC中以更大的响应返回数据。

目前无效:

$response->data; // returns null
$response[0]->data; // returns null
$response->data[0]; // returns null

感觉我错过了一些明显的东西。

2 个答案:

答案 0 :(得分:0)

首先使用json_decode对其进行解码,然后尝试访问数据对象

$res = json_decode($response);    
print_r($res->data);
print_r($res->data[0]->id);
print_r($res->data[0]->message);

code

答案 1 :(得分:0)

你必须使用第一个json解码函数,这样你才能从json响应中获取数据。

$response = '{
  "data": [
  {
     "id": "143384725674462_1592535354092718",
     "message": "Coming soon #PERFECTFIT 05.07.17 \nRegister to be one of the first to find out what it is here ⬇️\nhttp://www.bathrugby.com/the-club/supporters/perfect-fit-register/",
     "created_time": "2017-06-26T17:39:20+0000",
     "link": "http://www.bathrugby.com/the-club/supporters/perfect-fit-register/",
     "full_picture": "https://scontent.xx.fbcdn.net/v/t39.2147-6/19284954_1592534984092755_4946207882807869440_n.jpg?oh=56cc96435f423cec31962966b6f689c2&oe=59DB08B6"
  }
 ]
 }';

$response = json_decode($response);

print_r($response->data[0]->id); 
print_r($response->data[0]->message);