8

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!

1
  • You can see what's in the array or object with print_r($json_output); Commented Aug 3, 2010 at 8:17

2 Answers 2

8

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).

Sign up to request clarification or add additional context in comments.

Comments

5

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'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.