Recover Item json - Facebook API

Asked

Viewed 371 times

1

I’m working with the Facebook API and I’m trying to retrieve a json item, but I’m not getting it.

First that’s the json that’s returned to me from 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’m 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 to 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 pick up only messages posted by users, which would be the array message in the json.

I tried that:

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

But you returned all the messages instead of what you’re on message. The tricky thing is that it mounts several arrays. And I need arrays that contain only the message. In this case I posted only have a user post on the Facebook page. You will get more.

I need arrays that have only the message. Only complicates me this Facebook.

1 answer

2


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

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

If you want/prefer, you can check whether the current array contains the key message 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 per issue #4 of the question.

  • I don’t know if this is the way. message is not a array. When I make this code returns me error: Undefined index: message.

  • Yes, it is not an array, it is the key of a sub array returned from json. It returns the error because not all sub array has the key message. I added one more option in the reply so you can check before.

  • I had a change in data recovery, had to add the from. Now I need to retrieve the message and its from -> name. And ignore the others arrays not containing messages.

  • What I posted worked? I’ll modify it based on what you’ve edited now.

  • Yes, it worked. Thank you. The problem is in recovering the from.

  • I edited the answer according to issue #4 of your question. Make sure that’s what you need.

  • 1

    It’s exactly what I really need. Thank you, it worked. Now I’m going to research how to do a randomization of this array for me to show only 1 item each refresh.

  • 1

    You can give a shuffle($messages) and then a array_slice($messages, 1); There are other ways too.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.