user1816160
user1816160

Reputation: 11

Facebook app- getting name of a user doesn't work

I'm creating a Facebook app with the PHP SDK 3.2.0 and I encountered a problem. When I'm making an API call, trying to get name of a user nothing occours. I'm using the example from github.com and still can't achieve what I'm loking for.

require 'src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => 'xxx',
  'secret' => 'xxx',
));

// 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;
  }
} 

Upvotes: 1

Views: 354

Answers (2)

andyrandy
andyrandy

Reputation: 73984

Authenticated Referrals are deprecated and will be removed in February 2013.

That being said, i recommend using FB.login for user login:

https://developers.facebook.com/docs/reference/javascript/FB.login/

If you don´t want to use the Javascript SDK, there is also an easier way with the PHP SDK:

https://developers.facebook.com/docs/reference/php/facebook-getLoginUrl/

Everything is explained well in the docs. Just some more lines of code for you.

Upvotes: 1

Ivo Pereira
Ivo Pereira

Reputation: 3500

Well, you need to verify first if you have an active access token. If not you should show the Facebook login button.

<?php
$userId = $facebook -> getUser();

 if ($userId) { 
  $userInfo = $facebook->api('/' . $userId); ?>
  Welcome <?= $userInfo['name'] ?>
<?php } else { ?>
<div class="fb-login-button" data-show-faces="true" data-width="200" data-max-rows="1"></div>   
<?php } ?>

According to your code it should be like this:

<?php
require 'src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => 'xxx',
  'secret' => 'xxx',
));
?>
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'xxx', // App ID
      channelUrl : 'http://YOURDOMAIN.com/channel.php', // Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true  // parse XFBML
    });
    FB.Event.subscribe('auth.login', function(response) {
      window.location.reload();
      //alert(me.name);
    });
    }
</script>


<?php
$userId = $facebook -> getUser();

if ($userId) { 
  $userInfo = $facebook->api('/' . $userId); ?>
  Welcome <?= $userInfo['name'] ?>
<?php } else { ?>
<div class="fb-login-button" data-show-faces="true" data-width="200" data-max-rows="1"></div>   
<?php } ?>

    <script>
        // Load the SDK Asynchronously
        ( function(d) {
                var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
                if (d.getElementById(id)) {
                    return;
                }
                js = d.createElement('script');
                js.id = id;
                js.async = true;
                js.src = "//connect.facebook.net/en_US/all.js";
                ref.parentNode.insertBefore(js, ref);
            }(document));
    </script>

You should have a channel.php file in your server, that would have the following content:

<?php
$cache_expire = 60*60*24*365;
header("Pragma: public");
header("Cache-Control: max-age=".$cache_expire);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$cache_expire) . ' GMT');
?>
<script src="//connect.facebook.net/en_US/all.js"></script>

Upvotes: 1

Related Questions