0

I am using the following script from facebook example:

The script grabs the e-mail fine but hometown and location trigger a 500 error on my server. I need to grab the data for statistics.

<?php 

$app_id = "API_ID_GOES_HERE";
$app_secret = "SECRET_GOES_HERE";
$my_url = "REDIRECT_URL_GOES_HERE";
$permissions ="user_hometown,user_location,email";

session_start();
$code = $_REQUEST["code"];

if(empty($code)) {
 $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
 $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" 
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&scope=".$permissions. 
  "&state="
   . $_SESSION['state'];

 echo("<script> top.location.href='" . $dialog_url . "'</script>");
  }

if($_REQUEST['state'] == $_SESSION['state']) {
 $token_url = "https://graph.facebook.com/oauth/access_token?"
   . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
   . "&scope=".$permissions. "&client_secret=" . $app_secret . "&code=" . $code;

 $response = file_get_contents($token_url);
 $params = null;
 parse_str($response, $params);

 $graph_url = "https://graph.facebook.com/me?access_token=" 
   . $params['access_token'];

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

 echo("Hello " . $user->name);
 echo ("Location ". $user->location);


}
else {
  echo("The state does not match. You may be a victim of CSRF.");
}

?>
  • First do `print_r($user);` and check whether these parameters are coming or not. – Awais Qarni Oct 24 '11 at 09:56
  • I am getting the info including the information I requested permission for but they are displayed as follows for /me: stdClass Object ( [id] => 111806898837603 [name] => Adana, Turkey ) [location] => stdClass Object ( [id] => 106270412737566 [name] => Colorado Springs, Colorado ) So how do I get the stdClass Object via json (side note I am going to store them all as php variables) – Christopher Sparrowgrove Oct 24 '11 at 16:39
  • $user->hometown->name output is the name of the hometown. Thanks for your help. – Christopher Sparrowgrove Oct 24 '11 at 17:15

2 Answers2

2

I found the answer. Using this $user->hometown->name Will get the Hometown array and the Name object.

EX. [hometown] => stdClass Object ( [id] => 111806898837603 [name] => San Antonio, Texas )

$user->hometown->name  

OUTPUT:

San Antonio, Texas

  • My guess is it was triggering a 500 Error because I was echoing a stdClass Object in the wrong format or something. I know for sure it was triggered by using $user->hometown instead of $user->hometown->name – Christopher Sparrowgrove Oct 24 '11 at 17:16
1

I would take the URLs you are creating to get the user info off the graph api (i.e. $graph_url) and paste them into your browser to inspect the data facebook is returning, and go from there. If the URL's are returning proper information, then you know you are getting the right data back. Use the Graph API Explorer (https://developers.facebook.com/tools/explorer) to test the URL's you are creating for the graph API. You can see what results look like with different permissions granted and code accordingly for the response. Hope that helps!

kprager
  • 108
  • 4