Facebook连接API在代码点火器中返回空对象


Facebook connect API returning null object in codeigniter

在我的Codeigniter 2.1项目中,我在Facebook connect API中遇到了一个非常奇怪的问题。我的网站有社交登录功能,我在本地主机中测试的所有代码都运行得很好。但当我把我的项目上传到托管服务器时,Facebook连接库总是返回null对象。

我使用的是facebookphp-sdk 3.2,下面是我使用的代码。

在config文件夹中,我有一个名为facebook.php的文件,其中包含

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
$config = array(
    'appId' => 'xxxxxxxxxxxxxxxxxxxxxx',
    'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxx'
);
?>

我的库文件夹下有一个名为Fbconnect.php的库。包含代码

<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
include (APPPATH . 'libraries/facebook/facebook.php');
class Fbconnect extends Facebook {
    /**
     * Configured the facebook object
     */enter code here
    public $user = null;
    public $user_id = null;
    public $fb = false;
    public $fbSession = false;
    public $appkey = 0;
    public function __construct() {
        $ci = & get_instance();
        $ci->config->load('facebook', true);
        $config = $ci->config->item('facebook');
        parent::__construct($config);
        $this->user_id = $this->getUser();
        $me = null;
        if ($this->user_id) {
            try {
                $me = $this->api('/me');
                $this->user = $me;
            } catch (FacebookApiException $e) {
                error_log($e);
            }
        }
    }
}
?>

下面是我的控制器代码

    public function fb_login()
    {
    $this->load->library('fbconnect');
    $user_data = array(
        'redirect_uri' => actionLink("signup", 'social', 'Facebook'),
        'scope' => 'email'
    );
    redirect($this->fbconnect->getLoginUrl($user_data));
    }

我创建了一个名为actionLink的助手函数,它的代码是

function actionLink($controller = '', $action = '', $param = '')
{
    $url = base_url();
    if (strlen($controller) > 0) {
        $url .= "$controller";
        if (strlen($action) > 0) {
            $url .= "/$action";
            if (strlen($param) > 0) {
                $url .= "/$param";
            }
        }
    }
return $url;

}

请帮忙。

Signup.php中用以下内容替换第37至46行://inside function social($userType = 'Web')
1。尝试解决方案1只是为了确保它有效,但不是通用的,它是Facebook特有的
2。解决方案2与您所做的相同,但采用CI的方式:(


解决方案1

$this->load->library('fbconnect');
$socialId = $this->fbconnect->getSocialId();
$this->data['social_id'] = $socialId;
$email = $this->fbconnect->getEmail();
$this->data['email'] = $email;
$name = $this->fbconnect->getName();
$this->data['name'] = $name;
$this->data['url'] = $userType;


解决方案2

$className = $userType . "SocialService";
$this->load->library($className);
$socialId = $this->$className->getSocialId();
$this->data['social_id'] = $socialId;
$email = $this->$className->getEmail();
$this->data['email'] = $email;
$name = $this->$className->getName();
$this->data['name'] = $name;
$this->data['url'] = $userType;

实际上,您的主要问题出现在FacebookSocialService.php中。您试图以不正确的方式在公共方法中调用私有方法。这就是你无法获取任何细节的原因。解决方案如下:
在您的公共方法内部

public function getSomeData()
{
   $fbSocialService = new FacebookSocialService();
   $some_fb_data = $fbSocialService->getFacebook()->user['some_fb_data'];
   return $some_fb_data;
}
  • 但是,与其这样做,您还可以将私有方法设置为公共方法。((开玩笑,但它很快就解决了(:P