Retrieve Item jSON - Facebook API

1

I'm working with the Facebook API and I'm trying to retrieve an item from jSON, but I'm not getting it.

First of all, this is the jSON that is returned to me from the Graph Facebook API .

{
   "data": [
      {
         "message": "Boa Tarde!",
         "id": "**********************************",
         "created_time": "2015-09-24T19:10:13+0000"
         "from": {
             "name": "Diego Souza",
             "id": "**************************"
          },
      },
      {
         "name": "Mensagem",
         "id": "**********************************",
         "created_time": "2015-08-17T19:17:50+0000"
      },
      {
         "name": "Mensagem",
         "id": "**********************************",
         "created_time": "2015-07-17T13:06:12+0000"
      },
      {
         "name": "Mensagem",
         "id": "**********************************",
         "created_time": "2015-07-17T12:47:06+0000"
      },
      {
         "name": "Mensagem",
         "id": "**********************************",
         "created_time": "2015-07-17T12:41:41+0000"
      }
   ],
}

I am making this code:

# Posts Facebook
$authID         = "***************";
$authToken      = "***************";
$urlFace        = "https://graph.facebook.com/$authID/feed?fields=name,message&access_token=$authToken";
$jsonDados      = file_get_contents($urlFace);
$jsonObject     = json_decode($jsonDados, TRUE);

foreach ($jsonObject['data'] as $key => $value) {
    print_r($value);
}

And it's returning me like this:

Array
(
    [message] => Boa Tarde ! :)
    [from] => Array
        (
            [name] => Diego Souza
            [id] => **************************
        )

    [id] => **************************
    [created_time] => 2015-09-24T19:10:13+0000
)
Array
(
    [name] => Mensagem
    [id] => **********************************
    [created_time] => 2015-08-17T19:17:50+0000
)
Array
(
    [name] => Mensagem
    [id] => **********************************
    [created_time] => 2015-07-17T13:06:12+0000
)
Array
(
    [name] => Mensagem
    [id] => **********************************
    [created_time] => 2015-07-17T12:47:06+0000
)
Array
(
    [name] => Mensagem
    [id] => **********************************
    [created_time] => 2015-07-17T12:41:41+0000
)

What I need

In this case I would like to get only messages posted by users, which would be the array message in jSON .

I've tried this:

foreach ($jsonObject['data'] as $key => $value) {
    foreach($value as $var)
        echo $var;
}

But I returned all messages instead of what is in message . The trick is that it mounts multiple arrays. And I need arrays that contain only message . In that case that I post has only one user post on the Facebook page. There will be more.

I need arrays that have only message . It only complicates this Facebook.

    
asked by anonymous 30.09.2015 / 14:24

1 answer

2

In the way you are doing, you are saying to access all values of date . If you want to display only the message key, access it:

foreach ($json['data'] as $data) {
    print($data['message']);
}

If you want / prefer, you can check whether the current array contains the message key and assign it to a variable:

$messages = array();

foreach ($json['data'] as $data) {
   if(array_key_exists('message', $data)){
      $messages[$data['id']]['message']     = $data['message'];
      $messages[$data['id']]['from_name']   = $data['from']['name'];
      $messages[$data['id']]['from_id']     = $data['from']['id'];
   }
}

print_r($messages);

Will result in:

Array
(
    [******************] => Array
        (
            [message] => Boa Tarde!
            [from_name] => Diego Souza
            [from_id] => 100002341316240
        )
)

Edited as issue # 4 of the question.

    
30.09.2015 / 14:58