2012-02-26 29 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>";

这是错误消息:

无法连接到主机

但这工作。