Search code examples
facebookfacebook-graph-api

Show user's name on Facebook Page Tab


I have a fan page on Facebook and I'd like to show a christmas message to the people who like the page.

The idea is:

If user likes page > say: "Merry Christmas first_name" if user doesn't like the page > show generic message

I thought I'd be able to get the user's 1st name to display just that, since I don't want to store anything, but so far no success.

My code is:

require 'facebook.php';

$facebook = new Facebook(array(
  'appId'  => 'xx',
  'secret' => 'xx',
  'cookie' => true,
));

$session = $facebook->getSession();

$me = null;

if ($session) 
{
  try 
  {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
  } 
  catch (FacebookApiException $e) 
  {
    error_log($e);
  }
}

$name = $me['first_name'];
   
echo 'Merry Christmas x ' . $name . ' x ' . $uid;

But all it echoes is: "Merry Christmas x x 106998XX369535"

Where the number is the page's id, not the user's id.

Is there any way to do this without asking the user's permission?


Solution

  • I've found the answer.

    Just use: to show the user's name on a TAB/Canvas app.

    It also works within the Static FBML app.

    Writing:

    Merry Christmas <fb:userlink uid="loggedinuser"/>
    

    Will show:

    Merry Christmas John Smith
    

    if John Smith is viewing it :)

    The name will be formatted as a link, but you can style it with CSS.