Get Facebook real profile image URL
Asked Answered
C

6

11

According to Facebook graph API we can request a user profile picture with this (example):

https://graph.facebook.com/1489686594/picture

We don't need any Token since it's a public information.

But the real image URL of the previous link is: http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs356.snc4/41721_1489686594_527_q.jpg

If you type the first link on your browser, it will redirect you to the second link.

Is there any way to get the full URL (second link) with PHP, by only knowing the first link?

I have a function that gets the image from a URL to store it in the database, but it does work only if it get the full image URL.

Thanks

Chenoweth answered 20/8, 2010 at 22:8 Comment(6)
Yes, you can do it w-shadow.com/blog/2008/07/05/how-to-get-redirect-url-in-phpEnunciation
Oh gorgeous thanks a lot Ben, it works perfectly. I am using that so that when a user connects with Facebook on my website, I can grab their Facebook profile picture to make it their default one. Thanks :)Chenoweth
@kire, doesn't FaceBook developers API gives you an official solution to do this? I've never worked with it before but I think they do. Often taking over other websites resources without agreement could lead to an IP ban.Enunciation
@Enunciation what do you mean by "taking over other websites resources without agreement"? You mean it's bad to use or adapt a function shared by someone to help others? Don't think someone who share a server side function is expecting people to ask his agreement to understand and maybe use his function...Chenoweth
@kire, I don't mean the function I gave you, i mean Facebook's resources. Hot-linking images is ofter seen like a bad practice and/or rude if the source of the image isn't aware of what's happening. Read Facebook TOS and the Facebook Developers TOS in order to assure you can do this without using the API.Enunciation
@Enunciation Oh sorry mis-understood. I don't hotlink the pictures :) I just try to provide a way to users who connect with their Facebook account with a more user friendly experience. Like for example not having to re-upload their pictures. So their Facebook picture is copied over the new website. And users can choose to keep it or no of course. Noticed several app do that like FriendFeed for example (even though now it belongs to Facebook :))Chenoweth
G
20

kire is right, but a better solution for your use case would be the following:

    // get the headers from the source without downloading anything
    // there will be a location header wich redirects to the actual url
    // you may want to put some error handling here in case the connection cant be established etc...
    // the second parameter gives us an assoziative array and not jut a sequential list so we can right away extract the location header
    $headers = get_headers('https://graph.facebook.com/1489686594/picture',1);
    // just a precaution, check whether the header isset...
    if(isset($headers['Location'])) {
        $url = $headers['Location']; // string
    } else {
        $url = false; // nothing there? .. weird, but okay!
    }
    // $url contains now the url of the profile picture, but be careful it might very well be only temporary! there's a reason why facebok does it this way ;)
    // the code is untested!
Gomel answered 21/8, 2010 at 1:31 Comment(1)
Please consider adding information about getting the location in JSON via the Facebook Graph APIDugout
H
7

You can get it with FQL:

select pic_square from user where uid=1489686594

returns:

[
  {
    "pic_square": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs356.snc4/41721_1489686594_527_q.jpg"
  }
]

Also you can just improve your function that gets picture by url. If you use curl it can automatically follow redirect headers.

Haler answered 20/8, 2010 at 22:19 Comment(5)
I tried a similar solution but the Facebook "pic_square" propertie returns a 50px*50px, in my case I need a slighter bigger to not loose quality. I can only do it with the graph API by requesting this form: graph.facebook.com/xxxxxxxxx/picture?type=large But thanks a lot got a good solution now.Chenoweth
@Chenoweth there are 4 different pictures available, pick whichever you need: developers.facebook.com/docs/reference/fql/userHaler
Oh yeah true didn't find that page! Now the question for me is to know if it's better just request the Graph API URL and get the real picture URL, or use the FQL. Maybe the first solution would be more simple for me since I can add it to my Facebook class and call it anytime needed without need any Javascript or so over. You guys are really good in what you do :)Chenoweth
Oh sorry I could get the same results by querying that page: api.facebook.com/method/fql.query?query=SELECT pic_big FROM user WHERE uid = 1489686594 humm looks simpler...Chenoweth
@Chenoweth I love how somebody actually has "xxxxxxxxx" as a username on facebook and your example link resolves to his profile image.Armagnac
A
2

Please note that the The Surrican's answer (and possibly others) can drastically increase your script's response time (around +500ms for me on my server). This is because the server issues a request to facebook (because of get_headers()) and the execution time (computation excluded) extends from this:

  • browser -> server
  • server -> browser

to this:

  • browser -> server
  • server -> facebook (request: get_headers)
  • facebook -> server (response: get_headers)
  • server -> browser

This adds the mentioned 500ms delay. You probably should consider to cache the real url on your server or to load the profile picture via JavaScript. At least take a look at your response time before and after ;)

Alien answered 24/5, 2013 at 11:16 Comment(1)
Awesome observation about the response time. In conjunction with Graph API's JSON results, javascript would definitely be the best answer.Dugout
N
2

@The Surrican,
Nice code! Here is a clean cut code function for such process!

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

This will return the RAW facebook avatar image URL. Feel free to do whatever you want with it then!

Nudd answered 1/8, 2013 at 20:1 Comment(0)
V
0

You can also add ?redirect=false to the end of your URL and then parse the JSON response directly.

In your example: https://graph.facebook.com/1489686594/picture?redirect=false

More information here https://developers.facebook.com/docs/graph-api/reference/user/picture/

Vitamin answered 5/12, 2015 at 1:59 Comment(0)
G
0

You can just embed the image on your page using iframe as follows:

<iframe src="https://graph.facebook.com/<username>/picture"> </iframe> 

Replace 'username' with the profile's username.

Geisel answered 22/4, 2023 at 15:32 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.