JGallardo
JGallardo

Reputation: 11373

Facebook app not retrieving user info

I just built an app with the Facebook PHP SDK and set it up. I am testing it and though I get the image to show up from my profile, the user info requested is not showing up. Here is the screenshot of the results screenshot of the result

Here is the php code that I uploaded to the server. Note that the App ID and Secret have been removed from here.

<?php

require ('src/facebook.php');

$app_id = " "; //has been removed
$secret = " "; //has been removed
$app_url = "http://apps.facebook.com/instahomework/";

// Create our Application instance (replace this with your appId and secret). 
$facebook = new Facebook(array(
  'appId'  => $app_id,
  'secret' =>  $secret,
));

// Get User ID
$user = $facebook->getUser();

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl(); 
}
// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');

?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
    <title>php-sdk example 1 - user login and authorisation</title>
    <style>
        body { font-family: Helvetica, Verdana, Arial, sans-serif; }
        a { text-decoration:none; }
        a:hover { text-decoration:underline; }
    </style>
</head>
<body>
<pre>
<?php print_r($_REQUEST); ?>
</pre>
    <?php if ($user) { ?>
        <h2>Welcome</h2>
        <img src="https://graph.facebook.com/<?php echo $user; ?>/picture"><br/>
        <a href="<?php echo $link; ?>"><?php echo $name; ?></a><br/>
        <br/>
        Gender : <?php echo $gender; ?><br/>
        Locale : <?php echo $locale; ?><br/>
        Username : <?php echo $username; ?><br/>
    <?php } else { ?>
        <strong><em>You have not granted this App permission to access your data. Redirecting to permissions request...</em></strong>
<?php } ?>
</body>
</html>

Upvotes: 0

Views: 166

Answers (1)

howanghk
howanghk

Reputation: 3090

you should replace your HTML code with:

    <h2>Welcome</h2>
    <img src="https://graph.facebook.com/<?php echo $user; ?>/picture"><br/>
    <a href="<?php echo $user_profile['link']; ?>"><?php echo $user_profile['name']; ?></a><br/>
    <br/>
    Gender : <?php echo $user_profile['gender']; ?><br/>
    Locale : <?php echo $user_profile['locale']; ?><br/>
    Username : <?php echo $user_profile['username']; ?><br/>

as the "user info" you want is requested by this line of code:

$user_profile = $facebook->api('/me');

and stored into the $user_profile array.

Another workaround (but not recommended) is using extract() to extract the data from facebook into your global variable scope, by adding this line of code:

extract($user_profile);

Upvotes: 3

Related Questions