Reputation: 4487
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?
Upvotes: 2
Views: 5420
Reputation: 2493
$json = file_get_contents("http://graph.facebook.com/$uid");
$fdata = json_decode($json);
$fname = $fdata->name;
echo 'Merry Christmas x ' . $fname . ' x ';
Upvotes: 0
Reputation: 4487
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.
Upvotes: 1
Reputation: 18253
You cannot get any information about the user unless the user gives permission. Unfortunately.
But with "OAuth 2.0 for Canvas" you do receive a the signed_request POST. And this way is the only way to go about it; http://developers.facebook.com/docs/authentication/canvas
But user still needs to authorize your application.
Upvotes: 2