使用facebook连接从Facebook获取用户信息

时间:2012-06-14 12:27:42

标签: php facebook codeigniter

我正在实施facebook connect,它对我来说工作得很好。当用facebook登录时,它成功登录用户。这是登录http://beta-demo.info/party/users/loginByFacebook的链接,但在登录后,当我尝试从Facebook获取用户详细信息时,它现在显示任何内容。

以下是Fb_Connect.php的代码。此文件位于Libraries文件夹

<?php
include(APPPATH.'libraries/facebook/facebook.php');
class Fb_connect extends Facebook{

    //declare public variables
    public  $user           = NULL;
    public  $user_id        = FALSE;

    public $fb          = FALSE;
    public $fbSession   = FALSE;
    public $appkey      = 0;

    //constructor method.
    public function __construct()
    {
                $CI = & get_instance();
                $CI->config->load("facebook",TRUE);
                $config = $CI->config->item('facebook');
                parent::__construct($config);
                $this->user_id = $this->getUser(); // New code

                $me = null;
                if ($this->user_id) {
                    try {
                        $me = $this->api('/me');
                        $this->user = $me;
                        } catch (FacebookApiException $e) {
                            error_log($e);
                        }
        }
    } 

} // end class

这是记录功能

function loginByFacebook(){
            $this->load->library('fb_connect');
            $param['redirect_uri']=base_url();
            redirect($this->fb_connect->getLoginUrl($param));
    }

这是我试图获取值的信息函数。

function facebook() {   
print('<pre>');
print_r($this->fb_connect);
        if (!$this->fb_connect->user_id) {
        echo 'No working ';
            //Handle not logged in,
        } else {
        echo   $fb_uid = $this->fb_connect->user_id;
         echo  $fb_usr = $this->fb_connect->user;
           //Hanlde user logged in, you can update your session with the available data
           //print_r($fb_usr) will help to see what is returned
        }
}

信息页面的{0}}

的网址

我对facebook connect API没有太多了解。请帮助我

2 个答案:

答案 0 :(得分:1)

我唯一能看到你丢失的是调用'/ me'时的字段。这是我用来从facebook连接获取信息的功能。工作示例:http://www.paravegetarianos.com

function facebookConnect()
{
    $this->config->load('facebook', TRUE);
    $config = array(
        'appId'  => $this->config->item('facebook_api_id', 'facebook'),
        'secret' => $this->config->item('facebook_secret_key', 'facebook'),
        'fileUpload' => true,
    );
    $this->load->library('Facebook', $config);

    $user = $this->facebook->getUser();

    $profile = null;
    if($user):
        try {
            $profile = $this->facebook->api('/me?fields=id,username,email'); //<--- you are missing the fields
        } catch (FacebookApiException $e) {
            error_log($e);
            $user = null;
        }
    endif;

    if($profile != null):
        $profile['uid'] = $user;
        $data = $profile;
        var_dump($data); //<--------- Information
                    die();
    endif;  

}

答案 1 :(得分:0)

这是我的工作代码

<?php
   function loginByFacebook()
   {
        $this->load->library('fb_connect');      
        $param = array( 
                        'scope' =>'email,user_location,user_birthday,offline_access',                                                   'redirect_uri' => base_url()
                       );
        redirect($this->fb_connect->getLoginUrl($param));                   

   }

   function facebook()
   {    

        if (!$this->fb_connect->user_id) {      

        } else {        
            $fb_uid = $this->fb_connect->user_id;
            $fb_usr = $this->fb_connect->user;  
            $firstname = $fb_usr['first_name'];
   }
?>