phpjson

Problem reading the returned JSON into PHP usable arrays in a foreach loop


I don't really understand at all what I am doing wrong. But for the life of me I cannot access the information in the JSON string within a foreach loop.

Here is my code. (the json string contains a list of events for any given user. The user ID is passed in the URL as user=xxxxxx).

The Live URL is here http://livemuzik.co.uk/fb3.php

Code:

include('fb/src/facebook.php');

if ($_REQUEST["user"]){
$user_id = $_REQUEST["user"];
} else $user_id = "me";

$app_id = "xxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$my_url = "http://livemuzik.co.uk/fb2.php"; 

$code = $_REQUEST["code"];

if(empty($code)) {
    $auth_url = "http://www.facebook.com/dialog/oauth?client_id="
    . $app_id . "&redirect_uri=" . urlencode($my_url)
    . "&scope=create_event,user_events,friends_events";
    echo("<script>top.location.href='" . $auth_url . "'</script>");
}

$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$access_token = file_get_contents($token_url);

// GET JSON DATA FOR EVENTS
  $events_url  = "https://graph.facebook.com/";
  $events_url .= $user_id;
  $events_url .= '/events?fields=id,owner&limit=100&';
  $events_url .= $access_token;

$json = file_get_contents($events_url,0,null,null);

var_dump(json_decode($json, true));

// foreach(json_decode($json) as $obj) {
// Need to work out what code to put in here or what else I can do to output the json information as single variable values from an array
// }

The returned JSON appears like this:

array(2) {
  ["data"] => array(8) {
    [0] => array(4) {
      ["id"] => string(15) "159231904144232"
      ["owner"] => array(2) {
        ["name"]=> string(11) "Dean Hurley"
        ["id"]=> string(10) "1038439076"
      }
      ["start_time"]=> string(24) "2011-07-15T04:00:00+0000"
      ["rsvp_status"]=> string(9) "attending"
    }
    [1]=> array(4) { ["id"]=> string(15) "118572044893404" ["owner"]=> array(3) { ["name"]=> string(9) "RAVEOLOGY" ["category"]=> string(13) "Musician/band" ["id"]=> string(10) "8443998018" } ["start_time"]=> string(24) "2011-07-10T04:00:00+0000" ["rsvp_status"]=> string(6) "unsure" } [2]=> array(4) { ["id"]=> string(15) "123371994410260" ["owner"]=> array(3) { ["version"]=> int(1) ["name"]=> string(23) "The Bozeat City Rollers" ["id"]=> string(15) "182725898429985" } ["start_time"]=> string(24) "2011-07-03T04:00:00+0000" ["rsvp_status"]=> string(9) "attending" } [3]=> array(4) { ["id"]=> string(12) "316743171061" ["owner"]=> array(2) { ["name"]=> string(17) "Richard Johnstone" ["id"]=> string(9) "668431151" } ["start_time"]=> string(24) "2011-07-01T23:00:00+0000" ["rsvp_status"]=> string(6) "unsure" } [4]=> array(4) { ["id"]=> string(15) "160599624007096" ["owner"]=> array(2) { ["name"]=> string(11) "Dean Hurley" ["id"]=> string(10) "1038439076" } ["start_time"]=> string(24) "2011-06-15T08:30:00+0000" ["rsvp_status"]=> string(9) "attending" } [5]=> array(4) { ["id"]=> string(15) "231851770163680" ["owner"]=> array(2) { ["name"]=> string(11) "Dean Hurley" ["id"]=> string(10) "1038439076" } ["start_time"]=> string(24) "2011-06-13T08:30:00+0000" ["rsvp_status"]=> string(9) "attending" } [6]=> array(4) { ["id"]=> string(15) "203743706335174" ["owner"]=> array(2) { ["name"]=> string(15) "Bozeat Red Lion" ["id"]=> string(12) "155723533443" } ["start_time"]=> string(24) "2011-06-09T16:00:00+0000" ["rsvp_status"]=> string(9) "attending" } [7]=> array(4) { ["id"]=> string(15) "208151712549353" ["owner"]=> array(3) { ["name"]=> string(20) "Blackbush Promotions" ["category"]=> string(13) "Musician/band" ["id"]=> string(12) "336182297448" } ["start_time"]=> string(24) "2011-06-05T02:30:00+0000" ["rsvp_status"]=> string(6) "unsure" } } ["paging"]=> array(2) { ["previous"]=> string(181) "https://graph.facebook.com/1038439076/events?fields=id%2Cowner&limit=100&access_token=331843765383|b9ef3b78db6a708b1a735347.1-1038439076|DkL44VVbAPlHl8mb03P1WA9VF_o&since=1310702400" ["next"]=> string(181) "https://graph.facebook.com/1038439076/events?fields=id%2Cowner&limit=100&access_token=331843765383|b9ef3b78db6a708b1a735347.1-1038439076|DkL44VVbAPlHl8mb03P1WA9VF_o&until=1307241000" } }

My project involves synchronizing events and places to and from Facebook from Joomla Events management components.


Solution

  • This is wrong:

    foreach(json_decode($json) as $obj) {
    
    }
    

    it should be:

    $json = json_decode($json);
    
    foreach ($json->data as $data) {    
        echo $data->id . ' ' . htmlspecialchars($data->owner->name); 
    }