Facebook Graph API告诉我,我可以使用以下方式获取用户的个人资料图片:
http://graph.facebook.com/517267866/picture?type=large
这个方法很有效。然而,当你在浏览器中输入上述URL时,实际的图片地址是:
http://profile.ak.fbcdn.net/profile-ak-snc1/v227/560/83/n517267866_1928.jpg
我该如何通过编程方式获取第二个URL?
Facebook Graph API告诉我,我可以使用以下方式获取用户的个人资料图片:
http://graph.facebook.com/517267866/picture?type=large
这个方法很有效。然而,当你在浏览器中输入上述URL时,实际的图片地址是:
http://profile.ak.fbcdn.net/profile-ak-snc1/v227/560/83/n517267866_1928.jpg
我该如何通过编程方式获取第二个URL?
Location
头。但是,请注意不要依赖第二个URL是永久的。阅读一下HTTP响应代码(302而不是永久301),Facebook可能会定期更改这些URL以防止人们使用他们的服务器来托管图像等内容。
编辑:注意,原帖中的CDN URL现在是404,因此我们知道我们不能依赖URL具有长期存在性。另外,如果您从SSL安全页面上的<img>
链接到Graph API,有一个参数你必须添加请确保使用https://graph.facebook.com
。
redirect=false
- 这会导致返回JSON而不是重定向。返回的JSON包括CDN URL:{
"data": {
"url": "http://profile.ak.fbcdn.net/...",
"is_silhouette": false
}
}
再次强调,不要依赖于CDN URL的长期存在性。JSON响应带有宽松的CORS头,因此您可以使用XHR请求在客户端执行此操作。
content-disposition: attachment
和content-type: image/jpeg
。 - loretoparisihttp://graph.facebook.com/517267866/?fields=picture&type=large
该链接将返回JSON格式的URL。
我知道这有点晚了,但还有另一种方法可以获取个人资料图片的URL。
在原始的URL后面添加参数redirect=false
,即可获取通常会被重定向到的实际图片的URL。
所以,新的请求将看起来像这样:http://graph.facebook.com/517267866/picture?type=large&redirect=false。 这将返回一个JSON对象,其中包含图片的URL和一个布尔值is_silhouette(如果图片是默认Facebook图片,则为true)。
图片也将是您指定的尺寸。 您可以通过添加宽度和高度进行进一步测试:http://graph.facebook.com/517267866/picture?type=large&redirect=false&width=400&height=400
对于其他想在iOS中获取个人资料照片的人:
我刚刚通过以下步骤获取了用户的Facebook照片:
NSString *profilePicURL = [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture?type=large", fbUserID];
其中'fbUserID'是Facebook用户的个人资料ID。
这样,我总是可以调用profilePicURL中的url来获取图像,并且我总是能够获得它,没有问题。如果您已经拥有用户ID,则不需要进行任何API请求,只需将该ID插入到facebook.com/后面的url即可。
提醒需要在iOS中获取fbUserID的任何人:
if (FBSession.activeSession.isOpen) {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *user,
NSError *error) {
if (!error) {
self.userName = user.name;
self.fbUserID = user.id;
}
}];
}
https://graph.facebook.com/XXXXX/picture?height=1024
,浏览器将下载该图像而非显示它。有任何想法为什么会这样? - loretoparisi$url = 'http://graph.facebook.com/100000771470028/picture?type=large';
$rray=get_headers($url);
$hd = $rray[4];
echo(substr($hd,strpos($hd,'http')));
这将返回您所请求的URL,而Facebook更改URL的问题并不重要,因为您是从原始URL动态调用URL。
function getFacebookImageFromURL($url)
{
$headers = get_headers($url, 1);
if (isset($headers['Location']))
{
return $headers['Location'];
}
}
$url = 'https://graph.facebook.com/zuck/picture?type=large';
$imageURL = getFacebookImageFromURL($url);
这是唯一一个真正有效的方法:
me?fields=picture.type(*YOURTYPE*)
其中YOURTYPE可以是以下之一:small、normal、album、large、square。
针对Android:
根据最新的Facebook SDK,
首先您需要调用GraphRequest API
来获取用户的所有详细信息,其中API
还会提供当前个人资料图片的URL
。
Bundle params = new Bundle();
params.putString("fields", "id,email,gender,cover,picture.type(large)");
new GraphRequest(token, "me", params, HttpMethod.GET,
new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse response) {
if (response != null) {
try {
JSONObject data = response.getJSONObject();
if (data.has("picture")) {
String profilePicUrl = data.getJSONObject("picture").getJSONObject("data").getString("url");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).executeAsync();