Remove hassle of long register and login process and allow users to one click login for your site using facebook login. there are so many library available for add facebook login in codeigniter site. i am also sharing facebook login for codeigniter using graph api. there is no need to include any class or bundles cause we are not using php sdk we are contacting to facebook using graph api. so it’s so easy to add facebook login to codeigniter site. let’s see how to use facebook login in codeigniter.
1. Create an application on facebook step by step guide.
2. Create a view named “login.php” under “application/views/login.php” and add below code where you want to show facebook login button.
<a href="https://graph.facebook.com/oauth/authorize?client_id=YOUR_API_KEY&redirect_uri=<?php echo base_url('login');?>&scope=user_photos,email,user_birthday,user_hometown" class="facebook"></a>
3. Create a controller named “login.php” under “application/controllers/login.php” and add below code.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index(){
if (isset($_GET['code']) AND !empty($_GET['code'])) {
$code = $_GET['code'];
// parsing the result to getting access token.
parse_str($this->get_fb_contents("https://graph.facebook.com/oauth/access_token?client_id=YOUR_API_KEY&redirect_uri=" . urlencode(base_url('login')) ."&client_secret=YOUR_API_SECRET&code=" . urlencode($code)));
redirect('login?access_token='.$access_token);
}
if(!empty($_GET['access_token'])) {
// getting all user info using access token.
$fbuser_info = json_decode($this->get_fb_contents("https://graph.facebook.com/me?access_token=".$_GET['access_token']), true);
// you can get all user info from print_r($fbuser_info);
if(!empty($fbuser_info['email'])) {
echo $fbuser_info['first_name'];
echo $fbuser_info['last_name'];
echo $fbuser_info['email'];
echo $fbuser_info['gender'];
echo $fbuser_info['location']['name'];
echo $fbuser_info['hometown']['name'];
echo $fbuser_info['birthday'];
// do your stuff.
//save the data in db save session and redirect.
}
else{
$this->session->set_flashdata('message', 'Error while facebook user information.');
redirect(base_url());
}
}
if ($this->form_validation->run() == FALSE) {
$this->load->view('login'); // loading default view.
}
}
/**
* calling facebook api using curl and return response.
*/
function get_fb_contents($url) {
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, $url );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
$response = curl_exec( $curl );
curl_close( $curl );
return $response;
}
}
Note :- Replace YOUR_API_KEY and YOUR_API_SECRET to your facebook application api key and secret which you get on creating application.
Now need to add some code to db process on your end and you got facebook login on your site.