Reputation: 1
I've googled everywhere for this and I can't figure this out, so any help is much appreciated.
I have an iframe Facebook fan page and want the username to show up with some text. This is the code I have:
<?
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => '...',
'secret' => '...',
'cookie' => true
));
// 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;
}
}
?>
<? echo $user['name']; ?>
It only echoes "0"
I've been trying to do this with the php sdk, but if theres a way to do it with javascript that's fine too.
Upvotes: 0
Views: 1407
Reputation: 446
This works for me
$user= $facebook->getUser();
$fbme = $facebook->api('/me');
echo "Welcome".$fbme['name']."";
You can also use
$uid = $facebook->getUser();
$fbme= $facebook->api('/'.$uid);
where uid will be id for the app user.
Upvotes: 0
Reputation: 1569
You can also do something like:
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
And then you'll have:
$user['name'];
Upvotes: 1
Reputation: 25918
$user
in your sample code is a string containing user id from Facebook.
You probably wanted to print $user_profile['name']
Upvotes: 2