我正在开发一个CMS,它可以从用户的Facebook URL(即http://facebook.com/users_unique_url)中获取用户的个人资料图像。我怎样才能做到呢?是否有一个facebook API调用,获取用户的个人资料图像URL,而不需要用户允许应用程序?


当前回答

将此作为已接受答案的评论,但觉得它值得更长的解释。从2015年4月开始,这可能会提高几次。

As of V2 of the graph api the accepted answer no longer works using a username. So now you need the userid first, and you can no longer use a username to get this. To further complicate matters, for privacy reasons, Facebook is now changing userid's per app (see https://developers.facebook.com/docs/graph-api/reference/v2.2/user/ and https://developers.facebook.com/docs/apps/upgrading/#upgrading_v2_0_user_ids ), so you will have to have some kind of proper authentication to retrieve a userid you can use. Technically the profile pic is still public and available at /userid/picture (see docs at https://developers.facebook.com/docs/graph-api/reference/v2.0/user/picture and this example user: http://graph.facebook.com/v2.2/4/picture?redirect=0) however figuring out a user's standard userid seems impossible based just on their profile - your app would need to get them to approve interaction with the app which for my use case (just showing a profile pic next to their FB profile link) is overkill.

如果有人想出了一种方法来获得基于用户名的个人资料图片,或者,如何获得一个用户id(甚至是一个交替的)来检索个人资料图片,请分享!与此同时,旧的图形url在2015年4月之前仍然有效。

其他回答

获取图片URL,而不是二进制内容:

$url = "http://graph.facebook.com/$fbId/picture?type=$size";

$headers = get_headers($url, 1);

if( isset($headers['Location']) )
    echo $headers['Location']; // string
else
    echo "ERROR";

你必须使用你的FACEBOOK ID,而不是用户名。你可以在那里获得你的facebook id:

http://findmyfbid.com/

一种方法是使用Gamlet在他的回答中发布的代码:

保存为curl.php 然后在你的文件中: 需要“curl.php”; 美元的照片= " https://graph.facebook.com/me/picture?access_token= "。美元的会话(“access_token”); $sample = new sfFacebookPhoto; $thephotoURL = $sample->getRealUrl($photo); echo $ thephotoURL;

我想我应该张贴这个,因为我花了一些时间来弄清楚细节……即使个人资料照片是公开的,你仍然需要有一个访问令牌在那里,当你卷曲它。

简单的一行代码,保存完整尺寸的档案图像在您的服务器上。

<?php

copy("https://graph.facebook.com/FACEBOOKID/picture?width=9999&height=9999", "picture.jpg");

?>

只有在php.ini中启用了openssl,这才有效。

将此作为已接受答案的评论,但觉得它值得更长的解释。从2015年4月开始,这可能会提高几次。

As of V2 of the graph api the accepted answer no longer works using a username. So now you need the userid first, and you can no longer use a username to get this. To further complicate matters, for privacy reasons, Facebook is now changing userid's per app (see https://developers.facebook.com/docs/graph-api/reference/v2.2/user/ and https://developers.facebook.com/docs/apps/upgrading/#upgrading_v2_0_user_ids ), so you will have to have some kind of proper authentication to retrieve a userid you can use. Technically the profile pic is still public and available at /userid/picture (see docs at https://developers.facebook.com/docs/graph-api/reference/v2.0/user/picture and this example user: http://graph.facebook.com/v2.2/4/picture?redirect=0) however figuring out a user's standard userid seems impossible based just on their profile - your app would need to get them to approve interaction with the app which for my use case (just showing a profile pic next to their FB profile link) is overkill.

如果有人想出了一种方法来获得基于用户名的个人资料图片,或者,如何获得一个用户id(甚至是一个交替的)来检索个人资料图片,请分享!与此同时,旧的图形url在2015年4月之前仍然有效。

博客文章“抓取Facebook图形对象的图片”可能会提供另一种解决方案。使用教程中的代码以及Facebook的Graph API及其PHP SDK库。

... 并且尽量不要使用file_get_contents(除非您已经准备好面对后果——参见file_get_contents vs curl)。