如何从facebook sdk获取个人资料图片


How to get profile-picture from facebook sdk?

我需要从facebook获得个人资料图片,然后用户登录到web应用程序与facebook登录php sdk。然后把它变成

我使用这个代码:

 $fb = new Facebook'Facebook([
 'app_id' => 'xxx...x',
 'app_secret' => 'xx....xx',
 'default_graph_version' => 'v2.4',
 'default_access_token' => isset($_SESSION['facebook_access_token']) ? $_SESSION['facebook_access_token'] : 'xx..xx'
 ]);
 try {
 $response = $fb->get('/me?fields=id,name,email,picture');
 $user = $response->getGraphUser();
 $image = $user['picture'];
 echo "<img src='$image'  height='42' width='42'>";
 exit; //redirect, or do whatever you want
} catch(Facebook'Exceptions'FacebookResponseException $e) {
  //echo 'Graph returned an error: ' . $e->getMessage();
} catch(Facebook'Exceptions'FacebookSDKException $e) {
  //echo 'Facebook SDK returned an error: ' . $e->getMessage();
}

我怎样才能得到头像并正确显示?

出现错误?我正在检查facebook文档的示例,访问令牌作为参数通过调用get()

try {
  // Returns a `Facebook'FacebookResponse` object
  $response = $fb->get('/me?fields=id,name', '{access-token}');
} catch(Facebook'Exceptions'FacebookResponseException $e) {
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook'Exceptions'FacebookSDKException $e) {
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}
$user = $response->getGraphUser();
echo 'Name: ' . $user['name'];
https://developers.facebook.com/docs/php/howto/example_retrieve_user_profile/5.0.0

编辑。如果不工作,试试这个例子:http://www.howtobloger.com/2013/10/how-to-access-app-user-name-and-profile.html

您在$image中分配了数组picture:

"picture": {
    "data": {
      "height": 50,
      "is_silhouette": false,
      "url": "...",
      "width": 50
    }
  }

所以要检索url,你应该使用$image['url']。

这个问题是4年前的,这个答案是供参考的