我正在开发一个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月之前仍然有效。
其他回答
我在想,也许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/
更新:
从2012年8月底开始,API已经更新,允许您检索不同大小的用户头像。添加可选的宽度和高度字段作为URL参数:
https://graph.facebook.com/USER_ID/picture?width=WIDTH&height=HEIGHT
其中WIDTH和HEIGHT是您请求的维度值。
这将返回一个最小宽度x高度的头像,同时尝试保持纵横比。例如,
https://graph.facebook.com/redbull/picture?width=140&height=110
返回
{
"data": {
"url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash4/c0.19.180.142/s148x148/2624_134501175351_4831452_a.jpg",
"width": 148,
"height": 117,
"is_silhouette": false
}
}
最后更新
要获取用户的头像,请呼叫
https://graph.facebook.com/USER_ID/picture
其中USER_ID可以是用户id号或用户名。
要获取特定大小的用户头像,请调用
https://graph.facebook.com/USER_ID/picture?type=SIZE
哪里SIZE应该替换为其中一个单词
square
small
normal
large
这取决于你想要的尺寸。
这个调用将返回一个URL到单个图像,其大小基于您所选择的类型参数。
例如:
https://graph.facebook.com/USER_ID/picture?type=small
返回图像的一个小版本的URL。
API只指定配置文件图像的最大大小,而不是实际大小。
广场:
最大宽度和高度为50像素。
小
最大宽度为50像素,最大高度为150像素。
正常的
最大宽度为100像素,最大高度为300像素。
大
最大宽度为200像素,最大高度为600像素。
如果你调用默认的USER_ID/picture,你会得到方形类型。
澄清
如果你打电话(如上例所示)
https://graph.facebook.com/redbull/picture?width=140&height=110
如果你使用的是Facebook sdk的请求方法,它会返回一个JSON响应。否则它将返回图像本身。要始终检索JSON,添加:
&redirect=false
像这样:
https://graph.facebook.com/redbull/picture?width=140&height=110&redirect=false
获取图片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/
@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。那你想怎么处置就怎么处置吧!
显示:
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。
推荐文章
- 在foreach中通过引用传递
- 如何触发命令行PHP脚本的XDebug分析器?
- 如何找出如果你使用HTTPS没有$_SERVER['HTTPS']
- 更好的方法检查变量为null或空字符串?
- 当使用Composer的开发/生产开关时,如何正确部署?
- 自动删除Laravel (Eloquent ORM)中的相关行
- 在取消设置元素后重新设置数组键
- 如何修剪空白的数组值在php
- PHP中的双not(!!)操作符
- 在PHP5中创建单例设计模式
- 阻止人们入侵基于php的Flash游戏高分表的最佳方法是什么
- PHP子字符串提取。获取第一个'/'之前的字符串或整个字符串
- __construct函数的作用是什么?
- PHP中的异步shell执行器
- Laravel 5 -如何访问在视图存储上传的图像?