I’m trying to write PHP code that will display the logged in user’s name. So far I’ve got it to display the user’s id but I can’t get the name to show.
<?php
$app_id = "...";
$canvas_page = "...";
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page);
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode($payload), true);
if (empty($data["user_id"])){
echo("<script> top.location.href='" . $auth_url . "'</script>");
} else {
echo ("Welcome User: " . $data['user_id']);
}
?>
Changing ‘user_id’ to ‘name’ or ‘first_name’ doesn’t show anything.
You need to hit the
/meAPI to get user data.If using PHP, why don’t you check out the PHP SDK?