Get Facebook real profile image URL

后端 未结 5 1636
-上瘾入骨i
-上瘾入骨i 2020-12-08 17:40

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 T

相关标签:
5条回答
  • 2020-12-08 18:11

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

    0 讨论(0)
  • 2020-12-08 18:17

    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.

    0 讨论(0)
  • 2020-12-08 18:24

    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!
    
    0 讨论(0)
  • 2020-12-08 18:31

    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/

    0 讨论(0)
  • 2020-12-08 18:33

    @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!

    0 讨论(0)
提交回复
热议问题