2

好吧,所以几天以来,我一直在尝试将Facebook验证集成到我的基于Codeigniter的网站中。我已经将Facebook PHP SDK作为一个库包含进来,而且似乎连接起来很好,但是当我在SDK中使用getUser方法时,返回0表示我没有登录(除了我已登录到Facebook)。下面我将PHP SDK以库的形式包含进来,这个模型调用SDK中的方法,从模型中捕获数据的控制器,以及将信息显示给用户的视图。我希望大家能为我提供一些有关这个加重问题的内容。感谢您提前提供的所有帮助! (和公正的柜面亚勒想知道我使用的教程我在http://www.dannyherran.com/2011/02/facebook-php-sdk-and-codeigniter-for-basic-user-authentication/发现了一些代码):用Facebook(PHP SDK)和Codeigniter验证

FB PHP SDK库(存储在应用程序/库):

<?php //if (! defined('BASEPATH')) exit('No direct script access allowed'); 

/** 
* Copyright 2011 Facebook, Inc. 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); you may 
* not use this file except in compliance with the License. You may obtain 
* a copy of the License at 
* 
*  http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
* License for the specific language governing permissions and limitations 
* under the License. 
*/ 

require_once "base_facebook.php"; 

/** 
* Extends the BaseFacebook class with the intent of using 
* PHP sessions to store user ids and access tokens. 
*/ 
class Facebook extends BaseFacebook 
{ 
    /** 
    * Identical to the parent constructor, except that 
    * we start a PHP session to store the user ID and 
    * access token if during the course of execution 
    * we discover them. 
    * 
    * @param Array $config the application configuration. 
    * @see BaseFacebook::__construct in facebook.php 
    */ 
    public function __construct($config) { 
    if (!session_id()) { 
     session_start(); 
    } 
    parent::__construct($config); 
    } 

    protected static $kSupportedKeys = 
    array('state', 'code', 'access_token', 'user_id'); 

    /** 
    * Provides the implementations of the inherited abstract 
    * methods. The implementation uses PHP sessions to maintain 
    * a store for authorization codes, user ids, CSRF states, and 
    * access tokens. 
    */ 
    protected function setPersistentData($key, $value) { 
    if (!in_array($key, self::$kSupportedKeys)) { 
     self::errorLog('Unsupported key passed to setPersistentData.'); 
     return; 
    } 

    $session_var_name = $this->constructSessionVariableName($key); 
    $_SESSION[$session_var_name] = $value; 
    } 

    protected function getPersistentData($key, $default = false) { 
    if (!in_array($key, self::$kSupportedKeys)) { 
     self::errorLog('Unsupported key passed to getPersistentData.'); 
     return $default; 
    } 

    $session_var_name = $this->constructSessionVariableName($key); 
    return isset($_SESSION[$session_var_name]) ? 
     $_SESSION[$session_var_name] : $default; 
    } 

    protected function clearPersistentData($key) { 
    if (!in_array($key, self::$kSupportedKeys)) { 
     self::errorLog('Unsupported key passed to clearPersistentData.'); 
     return; 
    } 

    $session_var_name = $this->constructSessionVariableName($key); 
    unset($_SESSION[$session_var_name]); 
    } 

    protected function clearAllPersistentData() { 
    foreach (self::$kSupportedKeys as $key) { 
     $this->clearPersistentData($key); 
    } 
    } 

    protected function constructSessionVariableName($key) { 
    return implode('_', array('fb', 
           $this->getAppId(), 
           $key)); 
    } 
} 
?> 

的型号:

<?php 

/* 
* Copyright 2011 Facebook, Inc. 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); you may 
* not use this file except in compliance with the License. You may obtain 
* a copy of the License at 
* 
*  http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
* License for the specific language governing permissions and limitations 
* under the License. 
*/ 


class Facebook_model extends CI_Model { 

    public function __construct(){ 
     parent::__construct(); 

     $profile = null; 
     // Create our Application instance (replace this with your appId and secret). 
     $config = array(
         'appId' => 'MYAPPID', 
         'secret' => 'MYSECRET', 
         'fileUpload' => true, // Indicates if the CURL based @ syntax for file uploads is enabled. 
        ); 

     echo $this->load->library('Facebook', $config); 

     // Get User ID 
     $user = $this->facebook->getUser(); 

     // We may or may not have this data based on whether the user is logged in. 
     // 
     // If we have a $user id here, it means we know the user is logged into 
     // Facebook, but we don't know if the access token is valid. An access 
     // token is invalid if the user logged out of Facebook. 

     $profile = null; 
     if($user) 
     { 
      try { 
       // Proceed knowing you have a logged in user who's authenticated. 
       $profile = $this->facebook->api('/me?fields=id,name,link,email'); 
      } catch (FacebookApiException $e) { 
       error_log($e); 
       $user = null; 
      } 
     } 


     $fb_data = array(
         'me' => $profile, 
         'uid' => $user, 
         'loginUrl' => $this->facebook->getLoginUrl(
          array(
           'scope' => 'email,user_birthday,publish_stream', // app permissions 
           'redirect_uri' => '/profile' // URL where you want to redirect your users after a successful login 
          ) 
         ), 
         'logoutUrl' => $this->facebook->getLogoutUrl(), 
        ); 

     $this->session->set_userdata('fb_data', $fb_data); 

    } 

} 

?> 

控制器:

<?php 

    class The_facebook extends CI_Controller { 

     public function __construct(){ 
      parent::__construct(); 
      $this->load->model('facebook_model'); 
     } 

     public function index(){ 
      print_r($this->facebook); 
      $fb_data = $this->session->userdata('fb_data'); // This array contains all the user FB information 

      if((!$fb_data['uid']) or (!$fb_data['me'])) 
      { 
       // If this is a protected section that needs user authentication 
       // you can redirect the user somewhere else 
       // or take any other action you need 
       //redirect(base_url()); 
       echo "redirect";  

      } 
      else 
      { 
       $data = array(
         'fb_data' => $fb_data, 
        ); 

       $this->load->view('sand_view', $data); 
      } 

     } 

    } 

?> 

最后的视图:

<body> 
<div> 
    <?php if(!$fb_data['me']): ?> 
    Please login with your FB account: <a href="<?php echo $fb_data['loginUrl']; ?>">login</a> 
    <!-- Or you can use XFBML --> 
    <div class="fb-login-button" data-show-faces="false" data-width="100" data-max-rows="1" data-scope="email,user_birthday,publish_stream"></div> 
    <?php else: ?> 
    <img src="https://graph.facebook.com/<?php echo $fb_data['uid']; ?>/picture" alt="" class="pic" /> 
    <p>Hi <?php echo $fb_data['me']['name']; ?>,<br /> 
    <a href="<?php echo site_url('topsecret'); ?>">You can access the top secret page</a> or <a href="<?php echo $fb_data['logoutUrl']; ?>">logout</a> </p> 
    <?php endif; ?> 
</div> 
</body> 
+0

[使用Facebook PHP-SDK 3.x注册/登录Codeigniter 2.1.0用户](http://facebook.stackoverflow.com/questions/ 9454238 /使用facebook-php-sdk-3-x-to-register-login-user-with-codeigniter-2-1-0) – ifaour 2012-03-21 18:36:45

+0

我不会beli前夕如此,他的实现不起作用,除了它不能识别已登录的用户 – 2012-03-21 23:29:08

+0

我的实现**没有**工作。顺便说一句,'不起作用'是没有用的! – ifaour 2012-03-22 11:08:11

回答

1

我有同样的问题,我的破解是我将配置数组添加到Facebook类的构造函数,而不是作为参数传递给构造函数。出于某种原因,配置数组没有被传递或读取。

所以我砍看起来如下:

public function __construct() { 
    if (!session_id()) { 
     session_start(); 
    } 


    $config = array(
     'appId' => FACEBOOK_APP_ID, 
     'secret' => FACEBOOK_SECRET 
    ); 

    parent::__construct($config); 
} 

希望这有助于。

1

我一直在CodeIgniter的Facebook SDK上工作一段时间,并决定将我的代码作为开源扩展发布。 有一件事:这段代码使用SDK作为库,并在其上构建了一个模型,并准备使用多个其他令人敬畏的函数。

如果你想使用它,或以任何方式贡献(问题,承诺等),随时加入:

https://github.com/AlphabaseIT/CIconnect