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


当前回答

@NaturalBornCamper,

好的代码!这里是一个简洁的代码函数这样的过程!

function get_raw_facebook_avatar_url($uid)
{
    $array = get_headers('https://graph.facebook.com/'.$uid.'/picture?type=large', 1);
    return (isset($array['Location']) ? $array['Location'] : FALSE);
}

这将返回原始的Facebook头像图像URL。那你想怎么处置就怎么处置吧!

其他回答

我在想,也许ID会是一个有用的工具。每当用户创建一个新帐户时,它应该获得一个更高的ID。我谷歌了一下,发现有一种方法可以通过ID来估计帐户创建日期,Massoud Seifi从metadatascience.com收集了一些关于它的好数据。

阅读这篇文章:

http://metadatascience.com/2013/03/11/inferring-facebook-account-creation-date-from-facebook-user-id/

下面是一些可以下载的id:

http://metadatascience.com/2013/03/14/lookup-table-for-inferring-facebook-account-creation-date-from-facebook-user-id/

2015年4月开始的工作PHP (HTTP GET)解决方案(不含PHP 5 SDK):

function get_facebook_user_avatar($fbId){
        $json = file_get_contents('https://graph.facebook.com/v2.5/'.$fbId.'/picture?type=large&redirect=false');
        $picture = json_decode($json, true);
        return $picture['data']['url'];
}

你可以在参数中更改“类型”:

广场:

最大宽度和高度为50像素。

最大宽度为50像素,最大高度为150像素。

正常的

最大宽度为100像素,最大高度为300像素。

最大宽度为200像素,最大高度为600像素。

将此作为已接受答案的评论,但觉得它值得更长的解释。从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月之前仍然有效。

@NaturalBornCamper,

好的代码!这里是一个简洁的代码函数这样的过程!

function get_raw_facebook_avatar_url($uid)
{
    $array = get_headers('https://graph.facebook.com/'.$uid.'/picture?type=large', 1);
    return (isset($array['Location']) ? $array['Location'] : FALSE);
}

这将返回原始的Facebook头像图像URL。那你想怎么处置就怎么处置吧!

你关心头像大小吗?在使用PHP实现登录Facebook时。我们将向您展示在Facebook PHP SDK中获得大尺寸头像的简单方法。此外,您还可以获得自定义大小的Facebook个人资料的图像。

使用下面的代码行设置头像尺寸。

$userProfile = $facebook->api('/me?fields=picture.width(400).height(400)');

查看这个帖子:http://www.codexworld.com/how-to-guides/get-large-size-profile-picture-in-facebook-php-sdk/