使用Facebook PHP-SDK 3.x使用Codeigniter 2.1.0注册/登录用户 [英] Using Facebook PHP-SDK 3.x to register/login user with Codeigniter 2.1.0

查看:202
本文介绍了使用Facebook PHP-SDK 3.x使用Codeigniter 2.1.0注册/登录用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

转过Facebook API,我对正确的方法有些困惑。我想要用户跳过注册,或者如果他们用Facebook登录自动注册。所以如果他们登录到Facebook我收集他们的id,电子邮件和创建一个记录在我的用户表。

Going over the Facebook API and I'm a bit confused on the right approach. I want users to skip registration, or auto-register them if they sign in with Facebook. So if they sign into Facebook I collect their id, email and create a record in my user table.

如果用户表中已经存在一个id,注册并直接进入会员页面。这是我的代码到目前为止(取自Facebook的PHP SDK示例)。当我运行注册脚本时,页面显示为空白,我没有被重定向。

If an id exists already in the user table they skip the auto-registration and go directly to the members page. This is my code so far (taken from Facebook's PHP SDK example). When I run the signup script the page shows up as blank, I do not get redirected.

编辑:在需求之后似乎失败,如果我使用以下代码'test'从来没有被打印出来。

seems to be failing right after the require, if I use the following code 'test' never gets printed.

编辑:我正在使用Codeigniter,这个脚本是控制器的一部分,是否会导致需求出现问题? p>

I'm using Codeigniter and this script is part of a controller, would that cause a problem with the require?

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

    }


推荐答案

您阅读了文档框架。将库添加到Codeingiter是一个艰巨的任务,而不是。而Facebook图书馆也不例外。

I suggest you read the framework documentation. Adding a library to Codeingiter is not a hard task. And Facebook library is no exception.

这是一个快速整合,我刚刚提出:

Here's a quick integration I've just come up with:

1.创建配置文件: code> application / config / facebook.php

1.create a config file: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 / / code>并将 facebook.php 文件重命名为 Facebook.php ,并将此代码替换为php标签:

2.place the sdk files in the libraries folder application/libraries/ and rename the facebook.php file to Facebook.php and replace the php tag with this:

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

3.在您的控制器中加载配置文件,然后加载Facebook库:

3.in your controller load the config file and then load the Facebook library:

<?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

Now in the constructor method, you have just initialized the Facebook library (sdk) and it can be accessed by using: $this->facebook.

注意:


  • 您可以随时使用现有的图书馆,只是 Google it

  • 通常的做法是扩展核心Controller类,并在其中添加Facebook库初始化。

  • 或创建另一个库,扩展Facebook库,加载配置文件然后自动加载这个新的库。

这篇关于使用Facebook PHP-SDK 3.x使用Codeigniter 2.1.0注册/登录用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆