[英]Get facebook user's profile picture
This is my php code to get Facebook user profile image when I have logged in using facebook in my website. 这是我在网站上使用facebook登录后获取Facebook用户个人资料图像的php代码。 But photos is not displaying. 但是没有显示照片。 I am getting a broken image. 我的图像坏了。
<?php
session_start();
require_once 'vendor/autoload.php';
Facebook\FacebookSession::setDefaultApplication('appid', 'appcode');
$facebook = new Facebook\FacebookRedirectLoginHelper('http://example.com/index.php/');
try
{
if($session = $facebook->getSessionFromRedirect())
{
$_SESSION['facebook'] = $session->getToken();
header('Location: ../index.php');
}
if(isset($_SESSION['facebook']))
{
$session = new Facebook\FacebookSession($_SESSION['facebook']);
$request = new Facebook\FacebookRequest($session, 'GET', '/me');
$request = $request->execute();
$user = $request->getGraphObject(Facebook\GraphUser::className());
echo "Name: " . $user->getName();
echo "<br/>";
echo '<img src=\"https://graph.facebook.com/". $user. /picture?type=large\">';
}
}
catch(Facebook\FacebookRequestException $e)
{
echo " Error: " . $e->getMessage();
}
catch(\Exception $e)
{
}
?>
Simply use the following Graph path via GET request: 只需通过GET请求使用以下Graph路径:
/{user_id}?fields=picture.type(large),id,name
Field type can be one of the following values: 字段类型可以是以下值之一:
Or using width and/or height value like this: 或使用像这样的宽度和/或高度值:
/{user_id}?fields=picture.width(200).height(200),id,name
Also you can add redirect=0 param. 您也可以添加redirect = 0参数。 By default the picture edge will return a picture instead of a JSON response. 默认情况下,图片边缘将返回图片而不是JSON响应。 If you want the picture edge to return JSON that describes the image set redirect=0 when you make the request. 如果您希望图片边缘在发出请求时返回描述图像集的JSON的JSON,则JSON = 0。
So you will have JSON response: 这样您将获得JSON响应:
{
"picture": {
"data": {
"height": 120,
"is_silhouette": false,
"url": "https://scontent.xx.fbcdn.net/hprofile-xaf1/v/t1.0-1/c53.10.120.120/165675_138088076251005_752993_n.jpg?oh=a9450bf53f2d2294531e11ae28be99c1&oe=56C740A5",
"width": 120
}
},
"id": "138087416251071",
"name": "Zenonis",
}
If you set up a Facebook app, you have an "App ID" and an "App Secret" (which you specify as 'ID' and 'Code'). 如果设置了Facebook应用程序,则将具有“应用程序ID”和“应用程序机密”(将其指定为“ ID”和“代码”)。
You can use both these variables to get a user's picture with the following url: 您可以使用这两个变量来获取具有以下网址的用户图片:
$url = 'https://graph.facebook.com/v2.0/' . $user_id . '/picture?redirect=0&type=large&access_token=' . $app_id . '|' . $app_secret;
You can then print this url out in an src attribute: 然后,您可以在src属性中将该网址打印出来:
<img src="<?php print $url; ?>" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.