Get the User's name using FQL

This example covers getting specific profile fields for the current user and printing it using FQL and the Facebook SDK for PHP.

As of August 8 2016, FQL will no longer be available and cannot be queried. To migrate your app, use the API Upgrade Tool to see the Graph API calls you can make instead.

Learn more about the API v2.1 migration.

Old PHP SDK: This example is using an old version of the Facebook SDK for PHP (3.2.3).

  // Remember to copy files from the SDK's src/ directory to a
  // directory in your application on the server, such as php-sdk/

  $config = array(
    'appId' => 'YOUR_APP_ID',
    'secret' => 'YOUR_APP_SECRET',

  $facebook = new Facebook($config);
  $user_id = $facebook->getUser();

    if($user_id) {

      // We have a user ID, so probably a logged in user.
      // If not, we'll get an exception, which we handle below.
      try {
        $fql = 'SELECT name from user where uid = ' . $user_id;
        $ret_obj = $facebook->api(array(
                                   'method' => 'fql.query',
                                   'query' => $fql,

        // FQL queries return the results in an array, so we have
        //  to get the user's name from the first element in the array.
        echo '<pre>Name: ' . $ret_obj[0]['name'] . '</pre>';

      } catch(FacebookApiException $e) {
        // If the user is logged out, you can have a 
        // user ID even though the access token is invalid.
        // In this case, we'll get an exception, so we'll
        // just ask the user to login again here.
        $login_url = $facebook->getLoginUrl(); 
        echo 'Please <a href="' . $login_url . '">login.</a>';
    } else {

      // No user, so print a link for the user to login
      $login_url = $facebook->getLoginUrl();
      echo 'Please <a href="' . $login_url . '">login.</a>';