Retrieving a "Real" Facebook Profile Profile Image from an API Chart

The Facebook graph API tells me that I can get a profile picture of a user using

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

which works great. However, if you enter the above URL into the browser, the actual image address

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

How can I get the second url using the first program code?

+71
image facebook profile
Jul 27 '10 at 8:01
source share
12 answers

The first URL provides HTTP 302 (temporary redirect) to the second. So, to find the second URL programmatically, you can send an HTTP request for the first URL and get the Location header of the response.

However, do not rely on the second URL being pemanent. Read the HTTP response code a bit (302 as opposed to the constant 301), it is possible that Facebook changes these URLs on a regular basis so that people cannot, for example, use their servers to host images.




Edit: Please note that the CDN URL published by the OP is now 404, so we know that we cannot rely on the durability of the URL. In addition, if you are connecting to the Graph API with <img> on an SSL-protected page, there is a parameter there , you must add make sure that you are using http s ://graph.facebook.com .




Update: API added parameter - redirect=false - which leads to return JSON, and not to redirect. The recovered JSON includes the CDN URL:

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

Again, I would not rely on this CDN URL, which is durable. The JSON response is sent with permissive CORS headers, so you can do this client side with XHR requests.

+66
Jul 27 '10 at 8:10
source share
+36
Apr 10 2018-11-11T00:
source share

I understand this is late, but there is another way to get the profile image url.

You can add the redirect=false parameter to the source URL to get the actual URL of the image that you usually redirect to.

So, the new request will look like http://graph.facebook.com/517267866/picture?type=large&redirect=false . This will return a JSON object containing the image URL and a boolean is_silhouette (true if the image is a Facebook image by default).

The image will have the size you specified, as well. You can verify this further by adding dimensions: http://graph.facebook.com/517267866/picture?type=large&redirect=false&width=400&height=400

+15
04 Oct
source share

For anyone who wants to get a pic profile on iOS:

I just did this to get a custom Facebook pic:

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

where 'fbUserID' is the Facebook user profile identifier.

That way, I can always just call the url in profilePicURL to get the image, and I always get it, no problem. If you already have a user ID, you do not need any API requests, just paste the ID into the URL after facebook.com/.

FYI to anyone looking for fbUserID on iOS:

 if (FBSession.activeSession.isOpen) { [[FBRequest requestForMe] startWithCompletionHandler: ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) { if (!error) { self.userName = user.name; self.fbUserID = user.id; } }]; } 

To do this, you need an active FBSession (see Facebook docs and the Scrumptious example).

+7
Apr 03 '13 at 6:30
source share

If you want good quality JSON images with a URL, you can use this:

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

if you just need to use the image without redirecting the parameters:

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

517267866 is the profile identifier of one of the above examples. Put the facebook id you need

I hope this helps

+5
Jan 18 '17 at 9:22 on
source share
 $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 you specified and the problem of changing the facebook url does not matter because you are dynamically calling the url from the original url.

+3
Jul 13 '11 at 11:31
source share

this is the only thing that really works:

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

where YOURTYPE can be one of the following: small, normal, album, large, square

+2
Oct 19 '15 at 9:19
source share

For Android:

According to the latest Facebook SDK,

First you need to call the GraphRequest API to get all the details of the user, in which the API also provides the 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(); 
+2
Nov 26 '15 at 10:47
source share
 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); 
+1
Dec 04
source share

Now SSL is required to use Facebook

-> Important added S, https β†’ https://graph.facebook.com/userId/?fields=picture&type=large

Works in June / 2014

+1
Jun 30 '14 at 18:55
source share

Hmm..i tried everything to get the url for the image.The user the ideal solution was to use fql like this β†’

  $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 with big, pic_square to get other desired results. Hope this helps ....

0
Jul 03 '12 at 19:13
source share
 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); 

If id is your profile id .

0
May 18 '15 at 8:21
source share



All Articles