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.
I don’t know if this is the way.
message
is not aarray
. When I make this code returns me error:Undefined index: message
.– Diego Souza
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.
– Marcelo de Andrade
I had a change in data recovery, had to add the
from
. Now I need to retrieve themessage
and itsfrom -> name
. And ignore the othersarrays
not containingmessages
.– Diego Souza
What I posted worked? I’ll modify it based on what you’ve edited now.
– Marcelo de Andrade
Yes, it worked. Thank you. The problem is in recovering the
from
.– Diego Souza
I edited the answer according to issue #4 of your question. Make sure that’s what you need.
– Marcelo de Andrade
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.– Diego Souza
You can give a
shuffle($messages)
and then aarray_slice($messages, 1);
There are other ways too.– Marcelo de Andrade