Facebook API - 如何通过Facebook API获取Facebook用户的个人资料图片(无需用户"允许"申请)

Joh*_*man 255 php facebook

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

Sar*_*raz 408

只需通过以下URL获取数据:

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

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

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

资源:

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

注意:php.ini,您需要确保启用OpenSSL扩展以使用file_get_contentsPHP 的功能来读取该URL.

  • 进一步浏览该页面,另一个有用的东西可能是你可以添加的`?type = large`查询字符串.提出一个比我正在打字的屏幕抓取更好的答案的道具,BTW :). (73认同)
  • 应该强调的是,有些用户不会*为他们的FB帐户设置用户名.在这种情况下,如果您正在尝试实施此答案,您将收到FB的默认个人资料图片,即问号.要轻松避免此问题,请改用用户的FB ID. (39认同)
  • 它是2015年,我得到**(#803)无法通过用户名查询用户(sarfraz.anees)** (36认同)
  • 应该注意的是,在图api的V2中,这不再使用用户名.所以现在你首先需要userid,你不能再使用用户名来获取它.Userid也会针对每个应用进行更改,因此您必须进行某种正确的身份验证才能检索您可以使用的用户ID.从技术上讲,个人资料图片仍然是公开的,可以在http://graph.facebook.com/v2.2/4/picture?redirect=0上找到,但是根据用户的个人资料,找出用户ID(上一个链接中的4个)似乎是不可能的.旧的图表网址仍然有效,直到2015年4月. (7认同)
  • 使用`<img rel="nofollow noreferrer" src ="// graph.facebook.com/sarfraz.anees/picture"/>`如果您在HTTP和HTTPS之间进行更改...浏览器会检测到协议,您将无法获得混合内容HTTPS上的警告. (6认同)
  • @SantoshKumar*用户名*应替换为*user id*.`graph.facebook.com/ {用户ID}/picture` (4认同)
  • @Domenic:是的,新图API看起来很酷且很有前途:) (2认同)

nei*_*ker 267

显示:

50x50像素

<img src="//graph.facebook.com/{{fid}}/picture">
Run Code Online (Sandbox Code Playgroud)

宽度为200像素

<img src="//graph.facebook.com/{{fid}}/picture?type=large">
Run Code Online (Sandbox Code Playgroud)

保存(使用PHP)

注意:不要使用它.请参阅下面的@ Foreever评论.

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

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

注意:如果图像标记为"18+",您将需要来自18岁以上用户的有效access_token:

<img src="//graph.facebook.com/{{fid}}/picture?access_token={{access_token}}">
Run Code Online (Sandbox Code Playgroud)

2015年更新:

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

  • 在2015年7月,这不再有效(错误:"无法通过用户名查询用户") (7认同)
  • 以下是关于类型参数的更多信息:"您可以使用类型参数指定所需的图片大小,该参数应为方形(50x50),小(50像素宽,可变高度),正常(100像素宽,可变高度)之一),大(约200像素宽,可变高度)"通过[Facebook上的图谱API](http://developers.facebook.com/docs/reference/api/) (6认同)
  • 要使用此方法,必须将allow_url_fopen设置为on.默认情况下禁用allow_url_fopen指令.您应该了解启用allow_url_fopen指令的安全隐患.可以访问远程文件的PHP脚本可能容易受到任意代码注入的攻击.启用allow_url_fopen指令后,您可以编写打开远程文件的脚本,就像它们是本地文件一样. (3认同)

Gun*_*son 163

更新:

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

https://graph.facebook.com/USER_ID/picture?width=WIDTH&height=HEIGHT
Run Code Online (Sandbox Code Playgroud)

在哪里WIDTHHEIGHT是您要求的维度值.

这将返回最小尺寸为WIDTHx 的轮廓图片,HEIGHT同时尝试保留纵横比.例如,

https://graph.facebook.com/redbull/picture?width=140&height=110
Run Code Online (Sandbox Code Playgroud)

回报

    {
      "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
      }
   }
Run Code Online (Sandbox Code Playgroud)

结束更新

要获取用户的个人资料照片,请致电

https://graph.facebook.com/USER_ID/picture
Run Code Online (Sandbox Code Playgroud)

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

要获取特定大小的用户个人资料照片,请致电

https://graph.facebook.com/USER_ID/picture?type=SIZE
Run Code Online (Sandbox Code Playgroud)

其中SIZE应用一个关键词来代替

square
small
normal
large
Run Code Online (Sandbox Code Playgroud)

取决于你想要的大小.

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

例如:

https://graph.facebook.com/USER_ID/picture?type=small
Run Code Online (Sandbox Code Playgroud)

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

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

广场:

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

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

正常

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

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

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

澄清

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

https://graph.facebook.com/redbull/picture?width=140&height=110
Run Code Online (Sandbox Code Playgroud)

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

&redirect=false
Run Code Online (Sandbox Code Playgroud)

像这样:

https://graph.facebook.com/redbull/picture?width=140&height=110&redirect=false
Run Code Online (Sandbox Code Playgroud)

  • 这是2015年7月,这不再有效.(错误:"无法按用户名查询用户") (3认同)
  • @GunnarKarlsson你错过了`&redirect = false`来返回JSON数据而不是重定向到图像 (2认同)

Nat*_*per 20

要获取图像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";
Run Code Online (Sandbox Code Playgroud)

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

http://findmyfbid.com/

  • 你得到了所有错误的朋友:)这是你的Facebook ID,而不是用户名http://findmyfbid.com/ (3认同)

Arm*_*and 19

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

<?php

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

?>
Run Code Online (Sandbox Code Playgroud)

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


sck*_*ckd 10

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

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

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


ben*_*tan 9

一种方法是使用他的答案中发布的代码Gamlet:

  • 保存为 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;
    
    Run Code Online (Sandbox Code Playgroud)

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


小智 8

有办法做到这一点;)

感谢" 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);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)


小智 8

从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'];
}
Run Code Online (Sandbox Code Playgroud)

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

广场:

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

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

正常

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

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


Err*_*oid 7

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

在此输入图像描述

阅读这篇文章:

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/

  • 这如何回答这个问题? (2认同)

小智 6

博客文章抓取Facebook图形对象的图片可能会提供另一种形式的解决方案.使用教程中的代码以及Facebook的Graph API及其PHP SDK库.

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