简体   繁体   English

从图API获取“真正的”Facebook个人资料图片URL

[英]Getting the “real” Facebook profile picture URL from graph API

Facebook graph API tells me I can get a profile picture of a user using Facebook图形API告诉我,我可以获得用户使用的个人资料图片

http://graph.facebook.com/517267866/picture?type=large http://graph.facebook.com/517267866/picture?type=large

which works fine. 哪个工作正常。 However, when you type above URL into a browser, the actual address of the image is 但是,当您在浏览器中键入上述URL时,图像的实际地址为

http://profile.ak.fbcdn.net/profile-ak-snc1/v227/560/83/n517267866_1928.jpg http://profile.ak.fbcdn.net/profile-ak-snc1/v227/560/83/n517267866_1928.jpg

How can I get the second URL using the first one programmatically? 如何以编程方式使用第一个URL获取第二个URL?

The first URL gives a HTTP 302 (temporary redirect) to the second. 第一个URL为第二个URL提供HTTP 302(临时重定向)。 So, to find the second URL programatically, you could issue a HTTP request for the first URL and get the Location header of the response. 因此,要以编程方式查找第二个URL,您可以为第一个URL发出HTTP请求并获取响应的Location标头。

That said, don't rely on the second URL being pemanent. 也就是说,不要依赖第二个URL是永久性的。 Reading a little in to the HTTP response code (of 302 as opposed to a permanent 301), it is possible Facebook changes those URLs on a regular basis to prevent people from—for example—using their servers to host images. 稍微阅读HTTP响应代码(302而不是永久301),Facebook可能会定期更改这些URL,以防止人们 - 例如 - 使用他们的服务器来托管图像。


Edit: Notice that the CDN URL the OP posted is now a 404, so we know that we cannot rely on the URL being long-lived. 编辑:请注意,OP发布的CDN URL现在是404,因此我们知道我们不能依赖URL是长期存在的。 Also, if you're linking to the Graph API from an <img> on a SSL-secured page, there's a parameter you have to add make sure you use http s ://graph.facebook.com . 此外,如果您从SSL加密页面上的<img>链接到Graph API, 必须添加一个参数确保使用http s ://graph.facebook.com


Update: The API has added a parameterredirect=false – which causes JSON to be returned rather than a redirect. 更新: API 添加了一个参数 - redirect=false - 这会导致返回JSON而不是重定向。 The retruned JSON includes the CDN URL: 重新构建的JSON包含CDN URL:

{
   "data": {
      "url": "http://profile.ak.fbcdn.net/...",
      "is_silhouette": false
   }
}

Again, I wouldn't rely on this CDN URL being long-lived. 同样,我不会依赖这个CDN URL是长寿的。 The JSON response is sent with permissive CORS headers, so you're free to do this client-side with XHR requests. JSON响应通过允许的CORS头发送,因此您可以使用XHR请求自由地执行此客户端。

I realize this is late, but there is another way to obtain the URL of the profile image. 我意识到这已经很晚了,但还有另一种方法来获取配置文件图像的URL。

To your original url, you can add the parameter redirect=false to obtain the actual URL of the image you'd normally be redirected to. 对于原始网址,您可以添加参数redirect=false以获取通常重定向到的图像的实际网址。

So, the new request would look like http://graph.facebook.com/517267866/picture?type=large&redirect=false . 因此,新请求看起来像http://graph.facebook.com/517267866/picture?type=large&redirect=false This will return a JSON object containing the URL of the picture and a boolean is_silhouette (true if the picture is the default Facebook picture). 这将返回一个JSON对象,其中包含图片的URL和一个布尔值is_silhouette(如果图片是默认的Facebook图片,则为true)。

The picture will be of the size you specified, as well. 图片的大小也是您指定的大小。 You can test this additionally by adding dimensions: http://graph.facebook.com/517267866/picture?type=large&redirect=false&width=400&height=400 您可以通过添加尺寸来额外测试: http//graph.facebook.com/517267866/picture?type = alar&redirect = false&width = 400&high = 400

For anyone else looking to get the profile pic in iOS: 对于其他想要在iOS中获取个人资料照片的人:

I just did this to get the user's Facebook pic: 我这样做是为了得到用户的Facebook照片:

NSString *profilePicURL = [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture?type=large", fbUserID];

where 'fbUserID' is the Facebook user's profile ID. 其中'fbUserID'是Facebook用户的个人资料ID。

This way I can always just call the url in profilePicURL to get the image, and I always get it, no problem. 这样我就可以随时调用profilePicURL中的url来获取图像,我总是得到它,没问题。 If you've already got the user ID, you don't need any API requests, just stick the ID into the url after facebook.com/. 如果您已经拥有用户ID,则不需要任何API请求,只需在facebook.com/之后将ID粘贴到URL中即可。

FYI to anyone looking who needs the fbUserID in iOS: 对于那些在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;
         }
     }];
}

You'll need an active FBSession for that to work (see Facebook's docs, and the "Scrumptious" example). 你需要一个活跃的FBSession才能工作(参见Facebook的文档,以及“美味”的例子)。

If you want the JSON of a good quality profile picture with the URL you can use that: 如果你想要一个高质量的个人资料图片的JSON与URL,你可以使用:

http://graph.facebook.com/517267866/picture?height=1024&redirect=false http://graph.facebook.com/517267866/picture?height=1024&redirect=false

if you just need the picture use it without the parameter redirect: 如果你只是需要图片使用它没有参数重定向:

http://graph.facebook.com/517267866/picture?height=1024 http://graph.facebook.com/517267866/picture?height=1024

517267866 is the profile ID of one of the above examples. 517267866是上述示例之一的配置文件ID。 Put the facebook id that you need 把你需要的Facebook ID

I hope that helps 我希望有所帮助

$url = 'http://graph.facebook.com/100000771470028/picture?type=large';
$rray=get_headers($url);
$hd = $rray[4];
echo(substr($hd,strpos($hd,'http')));

This will return the url that you asked, and the problem of changing the url by facebook doesn't matter because you are dynamically calling the url from the original url. 这将返回您询问的网址,并且由facebook更改网址的问题无关紧要,因为您正在从原始网址动态调用网址。

this is the only one that really works: 这是唯一真正有效的方法:

me?fields=picture.type(*YOURTYPE*)

where YOURTYPE can be one of the following: small, normal, album, large, square 其中YOURTYPE可以是以下之一:小,普通,专辑,大,方形

For Android: 对于Android:

According to latest Facebook SDK, 根据最新的Facebook SDK,

First you need to call GraphRequest API for getting all the details of user in which API also gives URL of current Profile Picture . 首先,您需要调用GraphRequest API来获取用户的所有详细信息,其中API还提供URL of current Profile Picture

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();
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);

Now , Facebook need SSL 现在,Facebook需要SSL

-> Important added S, https -> https://graph.facebook.com/userId/?fields=picture&type=large - >重要添加了S,https - > https://graph.facebook.com/userId/?fields=picture&type=large

Works In June / 2014 工作于2014年6月

Hmm..i tried everything to get url to user image.The perfect solution was fql use like this-> 嗯..我尝试了一切来获取用户图像的URL。完美的解决方案是fql使用像这样 - >

    $fql_b = 'SELECT pic from user where uid = ' . $user_id;
    $ret_obj_b = $facebook->api(array(
                               'method' => 'fql.query',
                               'query' => $fql_b,
                             ));


             $dp_url =$ret_obj_b[0]['pic'];

replace pic by big,pic_square to get other desired results. 用big,pic_square替换pic以获得其他所需结果。 Hope IT HELPED.... 希望它有所帮助....

ImageView user_picture;
userpicture=(ImageView)findViewById(R.id.userpicture);
URL img_value = null;
img_value = new URL("http://graph.facebook.com/"+id+"/picture?type=large");
Bitmap mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
userpicture.setImageBitmap(mIcon1);

Where ID is one your profile ID . ID是您的个人资料ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM