You are here

public function FacebookAuthManager::getFbProfilePicUrl in Social Auth Facebook 8

Makes an API call to get the URL of user's Facebook profile picture.

Return value

string|false Absolute URL of the profile picture False if user did not have a profile picture on FB or an error occured.

File

src/FacebookAuthManager.php, line 272

Class

FacebookAuthManager
Contains all Simple FB Connect logic that is related to Facebook interaction.

Namespace

Drupal\social_auth_facebook

Code

public function getFbProfilePicUrl() {

  // Determine preferred resolution for the profile picture.
  $resolution = $this
    ->getPreferredResolution();

  // Generate FB API query.
  $query = '/me/picture?redirect=false';
  if (is_array($resolution)) {
    $query .= '&width=' . $resolution['width'] . '&height=' . $resolution['height'];
  }

  // Call Graph API to request profile picture.
  try {
    $graph_node = $this->client
      ->get($query)
      ->getGraphNode();

    // We don't download the FB default silhouettes, only real pictures.
    $is_silhouette = (bool) $graph_node
      ->getField('is_silhouette');
    if ($is_silhouette) {
      return FALSE;
    }

    // We have a real picture, return URL for it.
    return $graph_node
      ->getField('url');
  } catch (FacebookResponseException $ex) {
    $this->loggerFactory
      ->get('social_auth_facebook')
      ->error('Could not load Facebook profile picture URL. FacebookResponseException: @message', [
      '@message' => json_encode($ex
        ->getMessage()),
    ]);
  } catch (FacebookSDKException $ex) {
    $this->loggerFactory
      ->get('social_auth_facebook')
      ->error('Could not load Facebook profile picture URL. FacebookSDKException: @message', [
      '@message' => $ex
        ->getMessage(),
    ]);
  }

  // Something went wrong and the picture could not be loaded.
  return FALSE;
}