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


当前回答

通过这个URL简单地获取数据:

http://graph.facebook.com/userid_here/picture

将userid_here替换为您想获取照片的用户的id。你也可以使用HTTPS。

可以使用PHP的file_get_contents函数读取URL并处理检索到的数据。

资源:

http://developers.facebook.com/docs/api

注意:在PHP .ini中,您需要确保OpenSSL扩展已启用,以便使用PHP的file_get_contents函数读取该URL。

其他回答

获取图片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/

我在想,也许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/

显示:

50×50像素

<img src="//graph.facebook.com/{{fid}}/picture">

200像素宽度

<img src="//graph.facebook.com/{{fid}}/picture?type=large">

保存(使用PHP)

注意:不要使用这个。请看下面@ forever的评论。

$img = file_get_contents('https://graph.facebook.com/'.$fid.'/picture?type=large');
$file = dirname(__file__).'/avatar/'.$fid.'.jpg';
file_put_contents($file, $img);

$fid是你在Facebook上的用户id。

注意:对于标记为“18+”的图像,您将需要18+用户的有效access_token:

<img src="//graph.facebook.com/{{fid}}/picture?access_token={{access_token}}">

2015年更新:

Graph API v2.0不能使用用户名查询,您应该始终使用userId。

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

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

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

<?php

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

?>

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