如何使用 facebook graph api 显示用户个人资料图片?
我想在我的应用程序画布页面中显示用户个人资料图片,有没有办法使用图形 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
知道用户 ID,其个人资料图片的 URL 是:-
在 [UID] 的位置放置 $uid 变量,并且该 URL 可以传递到 flash
Knowing the user id the URL for their profile picture is:-
where in place of [UID] you place your $uid variable, and that URL can be passed to flash
要获得不同的尺寸,可以使用类型参数:
您可以使用类型参数指定所需的图片尺寸,该尺寸应该是正方形(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.
您还可以通过提供如下所示的参数来调整个人资料图片的大小。
也会起作用的。
You can also resize the profile picture by providing parameters as shown below.
would work too.
对我来说效果很好
Works fine for me
编辑:所以facebook又改变了!不再按用户名搜索 - 已修改下面的答案...
重定向到图像(真实)
,例如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...
redirects to the image (true)
e.g. https://graph.facebook.com/4/picture?redirect=false&height=200&width=200
Facebook Graph picture doc
这是对我有用的代码!
假设您正在进行有效的会话,
Thanx 会去找 Raine,你这个家伙!
Here is the code that worked for me!
Assuming that you have a valid session going,
Thanx goes to Raine, you da man!
我在使用 CURL 时获取个人资料照片时遇到问题。我有一段时间认为 Facebook API 的实现有问题,但我需要在我的 CURL 中添加一些内容,称为:
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:
一件非常重要的事情是,与其他 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.
返回的数据是图像类型的二进制数据。如果您使用 JavaScript 检索用户照片,请在 XMLHttpRequest 中获取 blob 类型的照片数据,然后从响应中检索 blob URL。供您参考:
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: