PHP SDK 3.1.1和JS SDK getUser()返回0

时间:2011-08-10 19:43:08

标签: php facebook

我刚刚尝试更新我的脚本,并且我一直为getUser()

返回0

这里有一些我改变的片段

旧代码

<?php

require_once("src/facebook.php");

class cfb
{
    public  $fb;
    public  $_fb_user;
    public  $_fb_details;
    public  $_fb_app_id         = '**************';
    private $_fb_app_secret     = '***************';
    private $_user;

    function __construct()
    {
        $this->fb = new Facebook(array(
        'appId'  => $this->_fb_app_id,
        'secret' => $this->_fb_app_secret,
        'cookie' => true,
        ));

        $this->_fb_user = $this->fb->getSession();

        $this->DB = new db();
        $this->_user = new user();
    }

    public function session_exists()
    {

        // see if there is a session stored, if so make sure the session is still good on facebooks end
        if($this->_fb_user) {

            // test if session is still good
            try
            {
                $me = $this->fb->api('/me');
            }
            catch(FacebookApiException $e){

                error_log($e);

            }

            if(!empty($me)) { 

                return true; 

            }

        } else { 

            return false;

        }
    }

<?php require_once("src/facebook.php"); class cfb { public $fb; public $_fb_user; public $_fb_details; public $_fb_app_id = '**************'; private $_fb_app_secret = '***************'; private $_user; function __construct() { $this->fb = new Facebook(array( 'appId' => $this->_fb_app_id, 'secret' => $this->_fb_app_secret, 'cookie' => true, )); $this->_fb_user = $this->fb->getSession(); $this->DB = new db(); $this->_user = new user(); } public function session_exists() { // see if there is a session stored, if so make sure the session is still good on facebooks end if($this->_fb_user) { // test if session is still good try { $me = $this->fb->api('/me'); } catch(FacebookApiException $e){ error_log($e); } if(!empty($me)) { return true; } } else { return false; } } 新代码

<?php

require_once("src/facebook.php");

class cfb
{
    public  $fb;
    public  $_fb_user;
    public  $_fb_details;
    public  $_fb_app_id         = '*****************';
    private $_fb_app_secret     = '********************';
    private $_user;

    function __construct()
    {
        $this->fb = new Facebook(array(
        'appId'  => $this->_fb_app_id,
        'secret' => $this->_fb_app_secret,
        ));

        $this->_fb_user = $this->fb->getUser();

        $this->DB = new db();
        $this->_user = new user();
    }

    public function session_exists()
    {

        // see if there is a session stored, if so make sure the session is still good on facebooks end
        if($this->_fb_user) {

            // test if session is still good
            try
            {
                $me = $this->fb->api('/me');
            }
            catch(FacebookApiException $e){

            echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';

            }

            if(!empty($me)) { 

                return true; 

            }

        } else { 

            return false;

        }
    }
这是我的js代码 <?php require_once("src/facebook.php"); class cfb { public $fb; public $_fb_user; public $_fb_details; public $_fb_app_id = '*****************'; private $_fb_app_secret = '********************'; private $_user; function __construct() { $this->fb = new Facebook(array( 'appId' => $this->_fb_app_id, 'secret' => $this->_fb_app_secret, )); $this->_fb_user = $this->fb->getUser(); $this->DB = new db(); $this->_user = new user(); } public function session_exists() { // see if there is a session stored, if so make sure the session is still good on facebooks end if($this->_fb_user) { // test if session is still good try { $me = $this->fb->api('/me'); } catch(FacebookApiException $e){ echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>'; } if(!empty($me)) { return true; } } else { return false; } }

我无法弄清楚。希望这将是简单的更新,但我猜不是= /

1 个答案:

答案 0 :(得分:2)

在您的JS代码中,在oauth:true函数中添加FB.init

您可以在此示例中看到PHP SDK v.3.1.1与JS SDK一起使用:https://developers.facebook.com/blog/post/534/