Using Facebook PHP-SDK 3.x to register/login user with Codeigniter 2.1.0

后端 未结 4 2641
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-09 07:10

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 th

4条回答
  •  悲&欢浪女
    2020-12-09 07:37

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

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

    1.create a config file:application/config/facebook.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:

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

    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 '
    '.htmlspecialchars(print_r($user_info, true)).'
    '; } catch(FacebookApiException $e) { echo '
    '.htmlspecialchars(print_r($e, true)).'
    '; $user = null; } } else { echo "facebook->getLoginUrl()}\">Login using Facebook"; } } }

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

    Notes:

    • You can always use an existing library, just google it
    • A common practice is to extend the core Controller class and add the Facebook library initialization there.
    • Or create another library, extend the Facebook library, load the config file there and then autoload this new library.

提交回复
热议问题