使用PHP和facebook API获取用户好友列表


Get user friends list using PHP and facebook API

所以我使用CodeIgniter框架,并试图获得用户朋友。我在身份验证方面没有问题,但当我尝试使用https://graph.facebook.com/me/friends?fields=gender,id,name&access_token=中的get方法获取用户朋友时,它会返回空数组。这是前端代码

<script>
$(function () {
    FB.init({
        appId: ****, // App ID
        channelUrl: '', // Channel File
        status: true, // check login status
        cookie: true, // enable cookies to allow the server to access the session
        xfbml: true  // parse XFBML
    });
});
jQuery('*[data-action="doFbLogin"]').on('click', function () {
    FB.login(function (response) {
        if (response.authResponse) {
            FB.api('/me/friends', function(response) {
                // loading message
                var intervalLoading = 0;
                setInterval(function() {
                    intervalLoading = ++intervalLoading % 4;
                    jQuery('*[data-message="fb_loading"]').html("Connecting, please wait" + Array(intervalLoading + 1).join("."));
                }, 400);
                // request
                jQuery.post('http://www.****.***/ajax/fb_connect', {
                        "user_id": response.id,
                        "full_name": response.name,
                        "first_name": response.first_name,
                        "last_name": response.last_name,
                        "email": response.email,
                        "gender": response.gender,
                        "token": response.token,
                        "friends": response.friends,
                        "current_url": 'http://*****',
                        "ref_url": 'http://*****'
                    },
                    function (data) {
                        data = JSON.parse(data);
                        if (data == true) {
                            jQuery('*[data-message="fb_loading"]').removeAttr('data-message').html('Connected!');
                            location.reload();
                        } else {
                            jQuery('*[data-message="fb_loading"]').removeAttr('data-message').html('Connection failed!');
                            setTimeout(function(){
                                location.reload();
                            }, 2000);
                        }
                    }
                );
            });
        } else {
            console.log('something went wrong');
        }
    }, {scope: 'email'});
});

这是我的后端连接:

$data = array(
        'fb-config' => array(
            'appId'  => FACEBOOK_API_ID,
            'secret' => FACEBOOK_SECRET
        )
    );
    $this->load->library('facebook', $data['fb-config']);
    $data['userData'] = $this->facebook->getUser();
    if ( $data['userData'] ) {
        try {
            $data['user_profile'] = $this->facebook->api('/me');
        } catch (FacebookApiException $e) {
            $data['user_profile'] = null; // return false, execution terminated.
            $this->facebook->destroySession();
        }
    }
$this->session->set_userdata(array('user_fb_token' => $this->facebook->getAccessToken()));
            print_r($this->users_model->getUserFacebookFriends());
            exit;

以及交友方法:

public function getUserFacebookFriends()
{
    // get facebook friends (crawl)
    $graph_link = 'https://graph.facebook.com/me/friends?fields=gender,id,name&access_token=';
    $user_friends = file_get_contents($graph_link . $this->session->userdata('user_fb_token'));
    $friendsArray = json_decode($user_friends, true);
    if ( is_array($friendsArray) ) {
        $data = array('friendsArray' => $friendsArray['data']);
    } else {
        $data = array('friendsArray' => FALSE);
    }
    return $data;
}

所以我不知道为什么这不起作用,有什么建议吗?

结果是正确的。从v2.0开始,您只能获得授权您的应用程序的好友:https://developers.facebook.com/docs/apps/changelog

这很可能是stackoverflow上被问得最多的问题,请在问之前使用搜索选项(谷歌、stackoverflow)。一个非常详细的答案在这个线程中,例如:Facebook Graph Api v2.0+-/me/founders返回空,或者只返回同样使用我的应用的朋友

我遇到了同样的问题。您可以使用FQL获取好友列表。但是FQL已被弃用,我认为它将在2-3个月内禁用。在v2.0+版本中,隐私政策发生变化后,您只能获得已安装您的应用程序的好友列表(他们授予了您应用程序的权限)。一般来说,在v2.0之前,根据隐私,用户负责自己的帐户和朋友的帐户。v2.0之后,用户只负责自己的配置文件。

使用v2.0,你只能收集朋友列表来向他们推荐你的应用程序,所以你必须改变推广应用程序的方式来收集朋友档案。

FQL文件

请求看起来像:

GET /fql?q=SELECT+uid2+FROM+friend+WHERE+uid1=me()&access_token=...

查询当然可以修改,如下所示(您可以添加/删除字段,甚至添加子查询):

SELECT+uid,+name,+first_name,+last_name,+pic_big,+birthday_date,+sex,+locale+FROM+user+WHERE+uid+IN+(SELECT+uid2+FROM+friend+WHERE+uid1=me())

但是您的帐户/申请必须在2014年注册(我希望我是正确的,但您可以查看文档)