Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Education with Facebook Graph API in PHP

I'm trying to get the education info from Facebook's graph API using stdclass. here's the array:

 "username": "blah",
   "education": [
      {
         "school": {
            "id": "[removed]",
            "name": "[removed]"
         },
         "year": {
            "id": "[removed]",
            "name": "[removed]"
         },
         "type": "High School"
      },
      {
         "school": {
            "id": "[removed]",
            "name": "[removed]"
         },
         "year": {
            "id": "[removed]",
            "name": "[removed]"
         },
         "type": "College"
      }
   ],

How can I use PHP to select the one with type "college"? Here's what I'm using to read it:

 $token_url = "https://graph.facebook.com/oauth/access_token?"
   . "client_id=[removed]&redirect_uri=[removed]&client_secret=[removed]&code=".$_GET['code']."";


 $response = file_get_contents($token_url);


 parse_str($response);

 $graph_url = "https://graph.facebook.com/me?access_token=" 
   . $access_token;


     $user = json_decode(file_get_contents($graph_url));

So the name would be $user->name.

I tried $user->education->school but that didn't work.

Any help would be appreciated.

Thanks!

like image 362
Zack Tanner Avatar asked May 30 '11 00:05

Zack Tanner


1 Answers

Education in your JSON document is an array (notice that its items are surrounded by [ ]), so what you have to do is:

// To get the college info in $college
$college = null;
foreach($user->education as $education) {
    if($education->type == "College") {
        $college = $education;
        break;
    }
}

if(empty($college)) {
    echo "College information was not found!";
} else {
    var_dump($college);
}

The result would be something like:

object(stdClass)[5]
  public 'school' => 
    object(stdClass)[6]
      public 'id' => string '[removed]' (length=9)
      public 'name' => string '[removed]' (length=9)
  public 'year' => 
    object(stdClass)[7]
      public 'id' => string '[removed]' (length=9)
      public 'name' => string '[removed]' (length=9)
  public 'type' => string 'College' (length=7)

An easier trick would be to use json_decode with the second param set to true, which forces the results to be arrays and not stdClass.

$user = json_decode(file_get_contents($graph_url), true);

If you go with arrays, you have to change the college retrieval foreach to:

foreach($user["education"] as $education) {
    if($education["type"] == "College") {
        $college = $education;
        break;
    }
} 

and the result will be:

array
  'school' => 
    array
      'id' => string '[removed]' (length=9)
      'name' => string '[removed]' (length=9)
  'year' => 
    array
      'id' => string '[removed]' (length=9)
      'name' => string '[removed]' (length=9)
  'type' => string 'College' (length=7)

Although both are valid, in my opinion you should go with arrays, they are easier and more flexible for what you want to do.

like image 132
yannis Avatar answered Sep 21 '22 19:09

yannis