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

发布于 2024-09-05 05:57:01 字数 891 浏览 5 评论 0原文

我想在我的应用程序画布页面中显示用户个人资料图片,有没有办法使用图形 api 来做到这一点?

我知道我可以使用 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'] 似乎不起作用,但我对图 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?

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,

$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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

下雨或天晴 2024-09-12 05:57:01

知道用户 ID,其个人资料图片的 URL 是:-

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

在 [UID] 的位置放置 $uid 变量,并且该 URL 可以传递到 flash

Knowing the user id the URL for their profile picture is:-

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

where in place of [UID] you place your $uid variable, and that URL can be passed to flash

意中人 2024-09-12 05:57:01

要获得不同的尺寸,可以使用类型参数:

您可以使用类型参数指定所需的图片尺寸,该尺寸应该是正方形(50x50)、小(50像素宽,可变高度)和大(大约200像素)之一像素宽,可变高度): http://graph.facebook.com/squall3d/picture?类型=大

to get different sizes, you can use the type parameter:

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.

So要识趣 2024-09-12 05:57:01

您还可以通过提供如下所示的参数来调整个人资料图片的大小。

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

也会起作用的。

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.

怪异←思 2024-09-12 05:57:01
  //create the url
  $profile_pic =  "http://graph.facebook.com/".$uid."/picture";

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

对我来说效果很好

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

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

Works fine for me

醉态萌生 2024-09-12 05:57:01

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

https://graph.facebook.com/{{UID}}/picture?redirect=false&height=200&width=200
  • UID = 个人资料或页面 ID
  • 重定向返回 json (false) 或
    重定向到图像(真实)
  • 高度和宽度是作物
  • 不需要身份验证

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

Facebook Graph 图片文档

EDIT: So facebook has changed it again! 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
  • redirect either returns json (false) or
    redirects to the image (true)
  • height and width is the crop
  • does not require authentication

e.g. https://graph.facebook.com/4/picture?redirect=false&height=200&width=200

Facebook Graph picture doc

迎风吟唱 2024-09-12 05:57:01

这是对我有用的代码!

假设您正在进行有效的会话,

//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 会去找 Raine,你这个家伙!

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!

微暖i 2024-09-12 05:57:01

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

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

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_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
赠意 2024-09-12 05:57:01

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

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.

比忠 2024-09-12 05:57:01

返回的数据是图像类型的二进制数据。如果您使用 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); 

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:

 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); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文