2012-02-26 76 views
5

查看Facebook API,我對使用正確的方法感到困惑。我希望用戶跳過註冊,或在用戶使用Facebook登錄時自動註冊。因此,如果他們登錄Facebook,我收集他們的ID,電子郵件並在我的用戶表中創建一條記錄。使用Facebook PHP-SDK 3.x使用Codeigniter 2.1.0註冊/登錄用戶

如果用戶表中已經存在一個ID,他們會跳過自動註冊並直接進入成員頁面。這是我迄今爲止的代碼(取自Facebook的PHP SDK示例)。當我運行註冊腳本時,頁面顯示爲空白,我沒有被重定向。

編輯:似乎失敗後的要求,如果我使用下面的代碼'測試'永遠不會被打印。

編輯:我使用Codeigniter,這個腳本是控制器的一部分,會導致需求的問題?

require 'http://localhost/facebook-php-sdk-6c82b3f/src/facebook.php'; 
echo "test"; 

-

public function signup() 
    { 
     require 'http://localhost/facebook-php-sdk-6c82b3f/src/facebook.php'; 

     // Create our Application instance (replace this with your appId and secret). 
     $facebook = new Facebook(array(
      'appId' => 'xxxxxxxxxxxxxxx', 
      'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxx', 
     )); 

     // Get User ID 
     $user = $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. 

     if ($user) 
     { 
      try 
      { 
       // Proceed knowing you have a logged in user who's authenticated. 
       $user_profile = $facebook->api('/me'); 
      } 
      catch (FacebookApiException $e) 
      { 
       error_log($e); 
       $user = null; 
      } 
     } 

     // Login or logout url will be needed depending on current user state. 
     if ($user) 
     { 
      $logoutUrl = $facebook->getLogoutUrl(); 
     } 
     else 
     { 
      $loginUrl = $facebook->getLoginUrl(array('scope' => 'email')); 
      redirect($loginUrl); 
     } 

     print_r($user_profile); 


     $this->load->model("user_model"); 
     $privileges = 1; 
     $loginLocation = ip2long($_SERVER['REMOTE_ADDR']); 
     $active = 1; 
     $this->user_model->add_user($user_profile->id, $user_profile->name, $user_profile->email, $loginLocation, $privileges, $active); 

    } 
+0

您是否嘗試過使用文件系統路徑而不是URL路徑的require? – BenOfTheNorth 2012-02-26 15:35:35

回答

12

我建議你閱讀框架documentation。向CodeIgniter添加一個庫是而不是一個艱鉅的任務。 Facebook庫也不例外。

這裏有一個快速的整合,我只是想出:

1.創建一個配置文件:application/config/facebook.php

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 
$config['appId'] = 'app_id'; 
$config['secret'] = 'app_secret'; 

2.place在庫中的SDK文件夾application/libraries/並重新命名facebook.php文件到Facebook.php並用這個替換php標記:

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

3.in your controller load the config文件,然後加載Facebook的庫:

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

class Welcome extends CI_Controller { 

    public function __construct() 
    { 
     parent::__construct(); 
     // Your own constructor code 
     $CI = & get_instance(); 
     $CI->config->load("facebook",TRUE); 
     $config = $CI->config->item('facebook'); 
     $this->load->library('facebook', $config); 
    } 

    public function index() 
    { 
     $user = $this->facebook->getUser(); 
     if($user) { 
      try { 
       $user_info = $this->facebook->api('/me'); 
       echo '<pre>'.htmlspecialchars(print_r($user_info, true)).'</pre>'; 
      } catch(FacebookApiException $e) { 
       echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>'; 
       $user = null; 
      } 
     } else { 
      echo "<a href=\"{$this->facebook->getLoginUrl()}\">Login using Facebook</a>"; 
     } 
    } 
} 

現在,在類的構造方法,你剛纔初始化Facebook的庫(SDK),它可以通過使用訪問:$this->facebook

注:

  • 您可以隨時使用現有的庫,只是google it
  • 通常的做法是擴展核心控制器類,並添加Facebook庫初始化那裏。
  • 或者創建另一個庫,擴展Facebook庫,在那裏加載配置文件,然後自動加載這個新庫。
+1

謝謝,那就是訣竅。我有一些閱讀要做。 – PhoenixDown 2012-02-26 18:07:11

+0

歡迎您! – ifaour 2012-02-26 18:52:30

+0

+1代表Codeigniter格式的簡單示例 – Zabs 2013-07-22 13:11:43

0

得到$user後,檢查會話參數與print_r($_SESSION)。之後,通過if(isset($_SESSION['some session var']))檢查一些變量。如果這種情況是真的,那麼導航到您的主頁與header("location:main.php"); exit;

0

不知道爲什麼,但它總是跑這條線:

echo "<a href=\"{$this->facebook->getLoginUrl()}\">Login using Facebook</a>";

這是錯誤消息:

無法連接到主機

但這工作。