skip to Main Content

I have a request to the Facebook Graph API that returns a post from a facebook account.

I am simply trying to access the data instance of the object returned in PHP but everything I have tried returns NULL.

Sample response

{
   "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"
      }
   ]
}

I want to get at the array of objects data provides so I can MVC the data returned in a larger response.

This doesn’t currently work:

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

Feel I’m missing something obvious.

2

Answers


  1. first decode it with json_decode then trying to access the data object

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

    code

    Login or Signup to reply.
  2. you have to use first json decode function so you can get the data from the json response.

    $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); 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search