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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3012905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:25:01  来源:igfitidea点击:

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

phpfacebookfacebook-graph-api

提问by Odyss3us

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 仍然很陌生,而且我可能犯了一些非常业余的错误!

回答by hndcrftd

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

回答by squall3d

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

回答by Deepak Thomas

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.

也会工作。

回答by Kannan Prasad

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

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

Works fine for me

对我来说很好用

回答by sidonaldson

EDIT: So facebook has changed it again! No more searching by username - have amended the answer below...

编辑:所以facebook又改变了它!不再按用户名搜索 - 修改了下面的答案...

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

e.g. 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 图片文档

回答by Odyss3us

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,你这个男人!

回答by adjwilli

I was having a problem fetching profile photos while using 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:

我在使用 CURL 时获取个人资料照片时遇到问题。我想了一段时间我的 Facebook API 实现有问题,但我需要在我的 CURL 中添加一点,称为:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

回答by Sanket Sahu

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. So, if you want to fetch the URL, you either need to read the response HTTP header or you can use FQLs.

一件非常重要的事情是,像其他 Graph API 请求一样,您不会在响应中获得 JSON 数据,而是调用将 HTTP REDIRECT 返回到个人资料图片的 URL。因此,如果要获取 URL,则需要读取响应 HTTP 标头,或者可以使用 FQL。

回答by Chathushka Dilhan

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. For your reference:

返回的数据为图像类型的二进制数据。如果您使用 JavaScript 检索用户照片,请在 XMLHttpRequest 中获取 blob 类型的照片数据,然后从响应中检索 blob URL。供你参考:

 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);