简体   繁体   English

如何使用facebook graph api显示用户个人资料图片?

[英]How can I display the users profile pic using the facebook graph api?

I would like to display the users profile picture inside of my applications canvas page, is there a way to do that using the graph api? 我想在我的应用程序画布页面内显示用户个人资料图片,有没有一种方法可以使用图形API做到这一点?

I know I can do it using FBML but I would also like to pass the profile pic to a flash game I am making, so I would have to get the profile pic from the api and send it as a variable, here is the code I have thus far, 我知道我可以使用FBML做到这一点,但我也想将个人资料图片传递给我正在制作的Flash游戏,因此我必须从api获取个人资料图片并将其作为变量发送,这是我的代码到目前为止,

$facebook = new Facebook(array(
    'appId'  => FACEBOOK_APP_ID,
    'secret' => FACEBOOK_SECRET_KEY,
    'cookie' => true,
    'domain' => 'myurl/facebook-test'
));

$session = $facebook->getSession();

        $uid = $facebook->getUser();
        $me = $facebook->api('/me');

        $updated = date("l, F j, Y", strtotime($me['updated_time']));

        echo "Hello " . $me['name'] . $me['picture'] . "<br />";
  echo "<div style=\"background:url(images/bg.jpg); width:760px; height:630px;\">" . "You last updated your profile on " . $updated . "</div>" . "<br /> your uid is" . $uid;

$me['picture'] does not seem to be working, but I am still very new to the graph api, and I am probably making a few very amateur mistakes! $me['picture']似乎没有用,但是我对图api还是很陌生,我可能犯了一些非常业余的错误!

Knowing the user id the URL for their profile picture is:- 知道用户ID的个人资料图片的URL是:

http://graph.facebook.com/[UID]/picture

where in place of [UID] you place your $uid variable, and that URL can be passed to flash 在[UID]的地方放置$ uid变量,然后可以将该URL传递给flash

to get different sizes, you can use the type parameter: 要获得不同的大小,可以使用type参数:

You can specify the picture size you want with the type argument, which should be one of square (50x50), small (50 pixels wide, variable height), and large (about 200 pixels wide, variable height): http://graph.facebook.com/squall3d/picture?type=large . 您可以使用type参数指定所需的图片大小,该参数应为正方形(50x50),小(宽50像素,可变高度)和大(宽约200像素,可变高度)之一: http:// graph .facebook.com / squall3d / picture?type = large

You can also resize the profile picture by providing parameters as shown below. 您还可以通过提供如下所示的参数来调整个人资料图片的大小。

https://graph.facebook.com/[UID]/picture?width=140&height=140

would work too. 也会工作。

  //create the url
  $profile_pic =  "http://graph.facebook.com/".$uid."/picture";

 //echo the image out
 echo "<img src=\"" . $profile_pic . "\" />"; 

Works fine for me 对我来说很好

EDIT : So facebook has changed it again! 编辑 :所以facebook又改变了它! No more searching by username - have amended the answer below... 不再按用户名搜索-已修改以下答案...

https://graph.facebook.com/{{UID}}/picture?redirect=false&height=200&width=200
  • UID = the profile or page id UID =个人资料或页面ID
  • redirect either returns json (false) or redirects to the image (true) 重定向返回json(false)或重定向到图像(true)
  • height and width is the crop 高度和宽度就是农作物
  • does not require authentication 不需要身份验证

eg https://graph.facebook.com/4/picture?redirect=false&height=200&width=200 例如https://graph.facebook.com/4/picture?redirect=false&height=200&width=200

Facebook Graph picture doc Facebook Graph图片文档

Here is the code that worked for me! 这是对我有用的代码!

Assuming that you have a valid session going, 假设您正在进行有效的会话,

//Get the current users id
$uid = $facebook->getUser();

//create the url
$profile_pic =  "http://graph.facebook.com/".$uid."/picture";

//echo the image out
echo "<img src=\"" . $profile_pic . "\" />";

Thanx goes to Raine, you da man! Thanx去了Raine,你这个家伙!

One very important thing is that like other Graph API request, you won't get the JSON data in response, rather the call returns a HTTP REDIRECT to the URL of the profile pic. 一个非常重要的事情是,像其他Graph API请求一样,您不会获得响应的JSON数据,而是该调用将HTTP REDIRECT返回到个人资料图片的URL。 So, if you want to fetch the URL, you either need to read the response HTTP header or you can use FQLs. 因此,如果要获取URL,则需要读取响应HTTP标头,也可以使用FQL。

I was having a problem fetching profile photos while using CURL. 使用CURL时提取个人资料照片时遇到问题。 I thought for a while there was something wrong my implementation of the Facebook API, but I need to add a bit to my CURL called: 我以为有一段时间我的Facebook API实现有问题,但我需要在CURL中添加一些名称:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

The returned data is the binary data of the image type. 返回的数据是图像类型的二进制数据。 If you use JavaScript to retrieve the user photo, please get the photo data as blob type in a XMLHttpRequest, and then retrieve the blob URL from the response. 如果使用JavaScript检索用户照片,请在XMLHttpRequest中以blob类型获取照片数据,然后从响应中检索blob URL。 For your reference: 供你参考:

 var request = new XMLHttpRequest;
 var photoUri=config.endpoints.graphApiUri + "/v1.0/me/photo/$value";
 request.open("GET",photoUri);
 request.setRequestHeader("Authorization","Bearer "+token);
 request.responseType = "blob";
 request.onload = function (){
        if(request.readyState == 4 && request.status == 200){
               var image = document.createElement("img");
               var url = window.URL || window.webkitURL;
               var blobUrl = url.createObjectURL(request.response);
               image.src = blobUrl;
               document.getElementById("UserShow").appendChild(image);
        }
 };
 request.send(null); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM