首页 文章

Facebook API - 如何通过Facebook API获取Facebook用户的 Profiles 图片(无需用户“允许”申请)

提问于
浏览
251

我正在开发一个CMS,用于从他们的Facebook URL(即http://facebook.com/users_unique_url)获取用户的 Profiles 图片 . 我怎么能做到这一点?是否有一个Faceboook API调用,用于获取用户的配置文件图像URL,而无需用户需要允许该应用程序?

14 回答

  • 2

    @NaturalBornCamper,

    好的代码!这是一个干净利落的代码功能!

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

    这将返回 raw Facebook头像图片网址 . 随便随便做任何你想做的事!

  • 406

    只需通过URL获取数据:

    http://graph.facebook.com/userid_here/picture

    userid_here 替换为您要获取照片的用户的ID . 您也可以使用HTTPS .

    您可以使用PHP的 file_get_contents 函数来读取该URL并处理检索到的数据 .

    Resource:

    http://developers.facebook.com/docs/api

    Note:php.ini 中,您需要确保启用OpenSSL扩展名以使用PHP的 file_get_contents 函数来读取该URL .

  • 8

    显示:

    50 x50像素

    <img src="//graph.facebook.com/{{fid}}/picture">
    

    200像素宽度

    <img src="//graph.facebook.com/{{fid}}/picture?type=large">
    

    保存(使用PHP)

    注意: Don't use this . 请参阅下面的@ Foreever评论 .

    $img = file_get_contents('https://graph.facebook.com/'.$fid.'/picture?type=large');
    $file = dirname(__file__).'/avatar/'.$fid.'.jpg';
    file_put_contents($file, $img);
    

    其中$ fid是您在Facebook上的用户ID .

    注意:如果图像标记为“18”,您将需要来自18位用户的有效access_token:

    <img src="//graph.facebook.com/{{fid}}/picture?access_token={{access_token}}">
    

    更新2015年:

    无法使用用户名查询Graph API v2.0,您应始终使用 userId .

  • 5

    UPDATE

    从2012年8月底开始,API已更新,允许您检索不同大小的用户 Profiles 照片 . 将可选的宽度和高度字段添加为URL参数:

    https://graph.facebook.com/USER_ID/picture?width=WIDTH&height=HEIGHT
    

    其中 WIDTHHEIGHT 是您请求的维度值 .

    在尝试保留纵横比时,这将返回最小尺寸为 WIDTH x HEIGHT 的 Profiles 照片 . 例如,

    https://graph.facebook.com/redbull/picture?width=140&height=110
    

    回报

    {
          "data": {
            "url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash4/c0.19.180.142/s148x148/2624_134501175351_4831452_a.jpg",
            "width": 148,
            "height": 117,
            "is_silhouette": false
          }
       }
    

    END UPDATE

    要获取用户的 Profiles 照片,请致电

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

    其中 USER_ID 可以是用户标识号或用户名 .

    要获取特定大小的用户 Profiles 照片,请致电

    https://graph.facebook.com/USER_ID/picture?type=SIZE
    

    其中 SIZE 应替换为其中一个单词

    square
    small
    normal
    large
    

    取决于你想要的大小 .

    此调用将URL返回到单个图像,其大小基于您选择的类型参数 .

    例如:

    https://graph.facebook.com/USER_ID/picture?type=small
    

    返回一个小版本图像的URL .

    API仅指定配置文件图像的最大大小,而不是实际大小 .

    广场:

    最大宽度和高度为50像素 .

    最大宽度为50像素,最大高度为150像素 .

    正常

    最大宽度为100像素,最大高度为300像素 .

    最大宽度为200像素,最大高度为600像素 .

    如果您调用默认的USER_ID /图片,则会获得方形类型 .

    CLARIFICATION

    如果你打电话(如上例所示)

    https://graph.facebook.com/redbull/picture?width=140&height=110
    

    如果您正在使用其中一种Facebook SDK请求方法,它将返回JSON响应 . 否则它将返回图像本身 . 要始终检索JSON,请添加:

    &redirect=false
    

    像这样:

    https://graph.facebook.com/redbull/picture?width=140&height=110&redirect=false
    
  • 263

    要获取图像URL,请不要使用二进制内容:

    $url = "http://graph.facebook.com/$fbId/picture?type=$size";
    
    $headers = get_headers($url, 1);
    
    if( isset($headers['Location']) )
        echo $headers['Location']; // string
    else
        echo "ERROR";
    

    您必须使用您的FACEBOOK ID,而不是USERNAME . 你可以在那里获得你的Facebook ID:

    http://findmyfbid.com/

  • 19

    简单的单行代码,用于在服务器上保存完整大小的配置文件图像 .

    <?php
    
    copy("https://graph.facebook.com/FACEBOOKID/picture?width=9999&height=9999", "picture.jpg");
    
    ?>
    

    这只有在php.ini中启用openssl时才有效 .

  • 6
    URI  = https://graph.facebook.com/{}/picture?width=500'.format(uid)
    

    您可以通过online facebook id finder tool获取配置文件URI

    您还可以传递类型参数,其中可能的值为small,normal,large,square .

    参考official documentation

  • 17

    添加此作为对已接受答案的评论,但认为它应该得到更长的解释 . 从2015年4月左右开始,这可能会提高几次 .

    从图表api的V2开始,接受的答案不再使用用户名 . 所以现在你首先需要userid,你不能再使用用户名来获取它 . 为了进一步复杂化,出于隐私原因,Facebook现在正在更改每个应用程序的用户ID(请参阅https://developers.facebook.com/docs/graph-api/reference/v2.2/user/https://developers.facebook.com/docs/apps/upgrading/#upgrading_v2_0_user_ids),因此您必须进行某种正确的身份验证才能检索可以使用的用户ID . 从技术上讲, Profiles 图片仍然是公开的,可以在/ userid / picture上找到(请参阅https://developers.facebook.com/docs/graph-api/reference/v2.0/user/picture上的文档和此示例用户:http://graph.facebook.com/v2.2/4/picture?redirect=0)但是根据他们的 Profiles 来确定用户的标准用户ID似乎是不可能的 - 您的应用需要让他们批准与应用程序的交互,对于我的用例(只显示他们的FB配置文件链接旁边的配置文件图片)是过度的 .

    如果有人想出了一种方法来获取基于用户名的 Profiles 图片,或者如何获得用户ID(甚至是交替的)用于检索 Profiles 图片,请分享!与此同时,旧的图表网址仍然有效,直到2015年4月 .

  • 1

    一种方法是在他的代码中使用代码Gamlet posted回答:

    • 另存为 curl.php

    • 然后在你的文件中:

    require 'curl.php';
    
    $photo="https://graph.facebook.com/me/picture?access_token=" . $session['access_token'];
    $sample = new sfFacebookPhoto;
    $thephotoURL = $sample->getRealUrl($photo);
    echo $thephotoURL;
    

    我想我会发布这个,因为我花了一些时间来弄清楚细节......即使 Profiles 图片是公开的,你仍然需要在那里有一个访问令牌来获取它curl它 .

  • 9

    有办法做到这一点;)

    感谢“http://it.toolbox.com/wiki/index.php/Use_curl_from_PHP_-_processing_response_headers”:

    <?php
    
        /**
         * Facebook user photo downloader
         */
    
        class sfFacebookPhoto {
    
            private $useragent = 'Loximi sfFacebookPhoto PHP5 (cURL)';
            private $curl = null;
            private $response_meta_info = array();
            private $header = array(
                    "Accept-Encoding: gzip,deflate",
                    "Accept-Charset: utf-8;q=0.7,*;q=0.7",
                    "Connection: close"
                );
    
            public function __construct() {
                $this->curl = curl_init();
                register_shutdown_function(array($this, 'shutdown'));
            }
    
            /**
             * Get the real URL for the picture to use after
             */
            public function getRealUrl($photoLink) {
                curl_setopt($this->curl, CURLOPT_HTTPHEADER, $this->header);
                curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, false);
                curl_setopt($this->curl, CURLOPT_HEADER, false);
                curl_setopt($this->curl, CURLOPT_USERAGENT, $this->useragent);
                curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 10);
                curl_setopt($this->curl, CURLOPT_TIMEOUT, 15);
                curl_setopt($this->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
                curl_setopt($this->curl, CURLOPT_URL, $photoLink);
    
                //This assumes your code is into a class method, and
                //uses $this->readHeader as the callback function.
                curl_setopt($this->curl, CURLOPT_HEADERFUNCTION, array(&$this, 'readHeader'));
                $response = curl_exec($this->curl);
                if (!curl_errno($this->curl)) {
                    $info = curl_getinfo($this->curl);
                    var_dump($info);
                    if ($info["http_code"] == 302) {
                        $headers = $this->getHeaders();
                        if (isset($headers['fileUrl'])) {
                            return $headers['fileUrl'];
                        }
                    }
                }
                return false;
            }
    
    
            /**
             * Download Facebook user photo
             *
             */
            public function download($fileName) {
                curl_setopt($this->curl, CURLOPT_HTTPHEADER, $this->header);
                curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($this->curl, CURLOPT_HEADER, false);
                curl_setopt($this->curl, CURLOPT_USERAGENT, $this->useragent);
                curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 10);
                curl_setopt($this->curl, CURLOPT_TIMEOUT, 15);
                curl_setopt($this->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
                curl_setopt($this->curl, CURLOPT_URL, $fileName);
                $response = curl_exec($this->curl);
                $return = false;
                if (!curl_errno($this->curl)) {
                    $parts = explode('.', $fileName);
                    $ext = array_pop($parts);
                    $return = sfConfig::get('sf_upload_dir') . '/tmp/' . uniqid('fbphoto') . '.' . $ext;
                    file_put_contents($return, $response);
                }
                return $return;
            }
    
            /**
             * cURL callback function for reading and processing headers.
             * Override this for your needs.
             *
             * @param object $ch
             * @param string $header
             * @return integer
             */
            private function readHeader($ch, $header) {
    
                //Extracting example data: filename from header field Content-Disposition
                $filename = $this->extractCustomHeader('Location: ', '\n', $header);
                if ($filename) {
                    $this->response_meta_info['fileUrl'] = trim($filename);
                }
                return strlen($header);
            }
    
            private function extractCustomHeader($start, $end, $header) {
                $pattern = '/'. $start .'(.*?)'. $end .'/';
                if (preg_match($pattern, $header, $result)) {
                    return $result[1];
                }
                else {
                    return false;
                }
            }
    
            public function getHeaders() {
                return $this->response_meta_info;
            }
    
            /**
             * Cleanup resources
             */
            public function shutdown() {
                if($this->curl) {
                    curl_close($this->curl);
                }
            }
        }
    
  • 7

    从2015年4月开始使用PHP(HTTP GET)解决方案(不使用PHP 5 SDK):

    function get_facebook_user_avatar($fbId){
            $json = file_get_contents('https://graph.facebook.com/v2.5/'.$fbId.'/picture?type=large&redirect=false');
            $picture = json_decode($json, true);
            return $picture['data']['url'];
    }
    

    您可以在参数中更改'type':

    广场:

    最大宽度和高度为50像素 .

    最大宽度为50像素,最大高度为150像素 .

    正常

    最大宽度为100像素,最大高度为300像素 .

    最大宽度为200像素,最大高度为600像素 .

  • 3

    我在想 - 也许 ID 将是一个有用的工具 . 每次用户创建新帐户时,都应获得更高的ID . 我用谷歌搜索,发现有一种方法可以通过ID估算帐户创建日期,而来自metadatascience.com的Massoud Seifi收集了一些关于它的好数据 .

    Enter image description here

    阅读这篇文章:

    http://metadatascience.com/2013/03/11/inferring-facebook-account-creation-date-from-facebook-user-id/

    以下是一些要下载的ID:

    http://metadatascience.com/2013/03/14/lookup-table-for-inferring-facebook-account-creation-date-from-facebook-user-id/

  • 162

    博客文章Grab the Picture of a Facebook Graph Object可能会提供另一种形式的解决方案 . 使用教程中的代码以及Facebook的Graph API及其PHP SDK库 .

    ...并尽量不要使用 file_get_contents (除非你准备好面对后果 - 见file_get_contents vs curl) .

  • 7

    您是否关注 Profiles 图片大小?在使用PHP实现登录Facebook时 . 我们将向您展示在Facebook PHP SDK中获取大尺寸图片的简单方法 . 此外,您还可以获得Facebook Profiles 的自定义尺寸图像 .

    使用以下代码行设置配置文件图片尺寸 .

    $ userProfile = $ facebook-> api('/ me?fields = picture.width(400).height(400)');

    查看这篇文章:http://www.codexworld.com/how-to-guides/get-large-size-profile-picture-in-facebook-php-sdk/

相关问题