php - 使用 Facebook PHP-SDK 3.x 通过 Codeigniter 2.1.0 注册/登录用户

标签 php facebook codeigniter facebook-php-sdk

查看 Facebook API,我对正确的方法有点困惑。我希望用户跳过注册,或者如果他们使用 Facebook 登录则自动注册。因此,如果他们登录 Facebook,我会收集他们的 ID、电子邮件并在我的用户表中创建一条记录。

如果 id 已经存在于用户表中,他们将跳过自动注册并直接转到成员(member)页面。到目前为止,这是我的代码(取自 Facebook 的 PHP SDK 示例)。当我运行注册脚本时,页面显示为空白,我没有被重定向。

编辑:似乎在要求后立即失败,如果我使用以下代码“测试”永远不会被打印。

编辑:我正在使用 Codeigniter 并且此脚本是 Controller 的一部分,这会导致需求出现问题吗?

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);

    }

最佳答案

我建议你阅读框架documentation .将库添加到 CodeIgniter 不是一项艰巨的任务。 Facebook 图书馆也不异常(exception)。

这是我刚刚想出的一个快速集成:

1.创建一个配置文件:application/config/facebook.php

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

2.将 sdk 文件放在库文件夹 application/libraries/ 并将 facebook.php 文件重命名为 Facebook.php 和用这个替换 php 标签:

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

3.在您的 Controller 中加载配置文件,然后加载 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
  • 常见的做法是扩展核心 Controller 类并在其中添加 Facebook 库初始化。
  • 或者创建另一个库,扩展 Facebook 库,在那里加载配置文件,然后自动加载这个新库。

关于php - 使用 Facebook PHP-SDK 3.x 通过 Codeigniter 2.1.0 注册/登录用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9454238/

相关文章:

PHP OOP : Handling multiple row outputs from a database, 最好的方法是什么?

facebook - FQL : Retrieving a list of All User's Friends that Have Authorized My App

html - 无法在状态更新中更改我的网站的Facebook图标

php - Codeigniter 无法删除 cookie

PHP/MySQL – 本地主机与 Web 服务

php - 修复旧的 MySQL 日期字段错误

php - 如何更改 Yii2 中 Controller 的默认 View ?

Facebook 照片的 Android 多部分/表单数据编码

mysql - 如何在 CodeIgniter 中有条件地使用不同的数据库

mysql - 如何在 View 中显示数据库中的数据?