I’m trying to learn how to use CURL and Json to access data sent from the Facebook graph api.
I’m using the following function which pulls the post data:
function loadFB($fbID){
$url="https://graph.facebook.com/".$fbID."/feed?limit=1&access_token=xxxx";
$c = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$page = json_decode(curl_exec($c));
curl_close($c);
$post=reset($page->data);
if($post->message == '') {
$post_msg = '<a href="' . $post->link . '">' . $post->name . '</a>';
} else {
$post_msg = $post->message;
}
return $post_msg;
}
Example JSOn data looks like this:
{
"data": [
{
"id": "xxxx",
"from": {
"name": "xxx",
"category": "xxx",
"id": "xxx"
},
"picture": "xxxxxxxxx",
"link": "xxxxxxx",
"name": "Event name goes here",
"properties": [
{
"text": "Friday, July 15, 2011 at 4:00pm"
},
{
"text": "Venue Name"
}
],
"icon": "http://static.ak.fbcdn.net/rsrc.php/v1/yW/r/r28KD-9uEMh.gif",
"type": "link",
"object_id": "xxx",
"created_time": "2011-06-23T06:46:17+0000",
"updated_time": "2011-06-23T06:46:17+0000",
"likes": {
"data": [
{
"name": "xxxx",
"category": "xxx",
"id": "xxxx"
}
],
"count": 1
}
}
],
"paging": {
"previous": "xxxxx",
"next": "xxx"
}
}
As it stands, I can retrieve the message of a page update, or if its an event I can retrieve the event name and the link for the event.
But what if I want to retrieve say the event date or the venue name? Its under another tier of ‘properties’.
With my code so far, I can access the first level of things with $post->message, but when I try $post->properties->text this doesn’t work – so I don’t understand how this works. On top of that, in the ‘properties’, there’s 2 ‘text’ which is adding to my confusion of how to access these things.
Any pointers?
$post->propertiesis an array so :