Facebook PHP SDK V5 integration using Codeigniter 3
In this post we will show you Facebook PHP SDK V5 integration using Codeigniter 3, hear for Facebook PHP SDK V5 integration using Codeigniter 3 we will give you demo and example for implement.
we will show you the simplest thanks to Facebook PHP SDK V5 integration using Codeigniter 3 or your CodeIgniter project. we’d like to form associate degree app in facebook before exploitation facebook SDK. we are able to produce associate degree app for complimentary simply by visiting following link. The steps ar simply easy and really simple therefore i’m skipping it. Once we tend to produce associate degree app we are going to be supplied with associate degree app id and app secret.
Now let’s begin integration it into our CodeIgniter project. the simplest means is thru musician and if we do not knowledge to feature musician in your CodeIgniter project visit my tutorial the simplest thanks to add musician into Codeigniter three.
Step one install facebook SDK v5 by the subsequent code composer require "facebook/graph-sdk ~5.0"
Now in we login read page produce a link for facebook login for Facebook PHP SDK V5 integration using Codeigniter 3
<p> <h2>Facebook PHP SDK V5 integration using Codeigniter 3 - onlinecode</h2> <a title="click for Facebook PHP SDK V5 integration using Codeigniter 3" href="<?php echo base_url();?>login/loginfb">Login with Facebook - Facebook PHP SDK integration using Codeigniter</a> </p>
The codes for Facebook login function inside login controller for Facebook PHP SDK V5 integration
function loginfb(){ $fb_data = new Facebook\Facebook([ 'app_id' => 'add your app id', 'app_secret' => 'add your app key', 'default_graph_version' => 'v2.5', ]); $fbhelper = $fb_data->getRedirectLoginHelper(); $datapermissions = ['email','publish_actions','user_birthday','user_location']; // get data For more data permissions like user location etc we need to send over application for review $fbloginUrl = $fbhelper->getLoginUrl('http://localhost/onlinecode/login/callbackfb', $datapermissions); header("location: ".$fbloginUrl); }
The above code will help you to redirect to facebook login page and after a user authenticates and give permission it will redirect to callbackfb function in login controller for Facebook PHP SDK V5 integration. The code for it is given below.
function callbackfb() { echo "<h2>Facebook PHP SDK V5 integration using Codeigniter 3</h2>"; $fb_data = new Facebook\Facebook([ 'app_id' => 'add your app id', 'app_secret' => 'add your app key', 'default_graph_version' => 'v2.5', ]); $fbhelper = $fb_data->getRedirectLoginHelper(); try { // get access Token for Facebook PHP SDK V5 integration $accessToken = $fbhelper->getAccessToken(); }catch(Facebook\Exceptions\FacebookResponseException $exception) { // hear Graph returns an error echo '<b>Graph returned an error: ' . $exception->getMessage().'</b>'; exit; } catch(Facebook\Exceptions\FacebookSDKException $exception) { // hear validation fails or other local issues echo '<b>Facebook SDK returned an error: ' . $exception->getMessage().'</b>'; exit; } try { // Facebook PHP SDK V5 integration // dada of the Facebook\GraphNodes\GraphUser // object for the current user. // If we provided a 'default_access_token' then '{access-token}' is optional. $response = $fb_data->get('/me?fields=id,name,email,first_name,last_name,birthday,location,gender', $accessToken); // echo "<pre>"; print_r($response); echo "</pre>"; } catch(Facebook\Exceptions\FacebookResponseException $exception) { // hear Graph returns an error echo '<b>ERROR: Graph :: ' . $exception->getMessage().'</b>'; exit; } catch(Facebook\Exceptions\FacebookSDKException $exception) { // hear validation fails or other local issues echo '<b>ERROR: validation fails :: ' . $exception->getMessage().'</b>'; exit; } // dada of User Information about Retrival begins $me_data = $response->getGraphUser(); $location = $me_data->getProperty('location'); echo "User Full Name: ".$me_data->getProperty('name')."<br>"; echo "User First Name: ".$me_data->getProperty('first_name')."<br>"; echo "User Last Name: ".$me_data->getProperty('last_name')."<br>"; echo "User Gender: ".$me_data->getProperty('gender')."<br>"; echo "User Email: ".$me_data->getProperty('email')."<br>"; echo "User location: ".$location['name']."<br>"; echo "User Birthday: ".$me_data->getProperty('birthday')->format('d/m/Y')."<br>"; echo "User Facebook ID: <a href='https://www.facebook.com/".$me_data->getProperty('id')."' target='_blank'>".$me_data->getProperty('id')."</a>"."<br>"; $profile_id = $me_data->getProperty('id'); echo "</br><img alt='User profile id' src='//graph.facebook.com/$profile_id/picture?type=large'> "; echo "</br></br>User Access Token : </br>".$accessToken; }
The on top of operate can fetch basic user data from the facebook and displays it.The sample output is as below.
Facebook PHP SDK V5 integration using Codeigniter output ::

Hope this code and post will helped you for implement Facebook PHP SDK V5 integration using Codeigniter output. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs onlincode.org