我需要从Facebook获取个人资料图片然后用户登录到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();
}
如何获取个人资料图片并正确显示?
答案 0 :(得分:0)
有些错误?我正在检查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
答案 1 :(得分:0)
您在$ image处分配了阵列图片:
"picture": {
"data": {
"height": 50,
"is_silhouette": false,
"url": "...",
"width": 50
}
}
因此,要检索url,应使用$ image ['url']。
PS问题来自4年前,这个答案仅供参考