tags:

views:

272

answers:

2

Hiya! http://developers.facebook.com/docs/reference/api/event

I'm trying to grab some values from a Facebook JSON even object in PHP. Namely, title of event, location, and people attending. Using the Graph API.

<?php

$jsonurl = "https://graph.facebook.com/331218348435?access_token=2227470867|2.rtBZMkVIVgKGZ7Xr4px3Dw__.3600.1280822400-662817093|apY_UHK_2SKQFel3XxpKJ09GEo4.";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);

How can echo the values from JSON output? I'm assuming it will be returned as an array.

Thank you!

A: 

Specify the second argument of true to json_encode to convert it to array and then you can print the output like this:

$json_output = json_decode($json, true);
echo '<pre>';
print_r($json_output);
echo '</pre>';

Now you can get specific item like this:

echo $json_output['title'];
Sarfraz
A: 

If you're using PHP, you can use the PHP SDK to query the API. This means you can make calls like:

$user = $facebook->api('/someusername', array('fields' => 'id,first name,last_name ...'));

However, with your example, you could do the following:

$url  = "https://graph.facebook.com/331218348435?access_token=2227470867|2.rtBZMkVIVgKGZ7Xr4px3Dw__.3600.1280822400-662817093|apY_UHK_2SKQFel3XxpKJ09GEo4.";
$user = json_decode(file_get_contents($jsonurl));

echo $user['first_name'];

As if you use json_decode, it should decode the result into a native PHP array (or in same cases, an object).

Martin Bean