Skip to content
Advertisement

Facebook API php: check if username is set or not

In which way could I check if FaceBook profile username is set or not? Because I’ve created a FaceBook profile for do some tests, but it seems that no username is found from profile.

this is my code:

$token = $_POST['token'];
$fb_username;
   //get username by GraphApi
  $facebook->setAccessToken($token);
  $user_id = $facebook->getUser();

  try {

  $user_profile = $facebook->api('/me', 'GET');
 
  $fb_username = $user_profile['username'];
  } catch (FacebookApiException $e) {
  sendError("Internal error", 0);
  exit(1);
  }

Advertisement

Answer

This is probably you never gave your test profile a username. you can give a username to your facebook profile by following these steps:

1. Click  at the top right of any Facebook page and choose Settings
2. Click General in the left column
3. Look for the setting Username and click Edit to the far right
4. Type your new username in the open field and click Save Changes

you could test if a username is there with

$usernameExists = isset($user_profile['username']) && $user_profile['username'];
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement