I tried to integrate Facebook sdk v5 to my codeigniter project, by adding it to library and trying to override Facebook.php file but it didn't works. How can i integrate it in a correct way. My goal is to get the number of likes from a page, i'm the administrator.
2
votes
2 Answers
5
votes
I will suggest You to integrate using as a model . Unzip you facebook sdk . Put the unzipped folder in application/models . Rename the sdk folder to facebook . Now you can use the following code to instantiate FB and get a kick-start . You can use this model for querying facebook .
define('FACEBOOK_SDK_V4_SRC_DIR',APPPATH.'/models/facebook/');
require_once("facebook/autoload.php");
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;
class facebook_model extends CI_Model
{
public $fb ;
public $access_token ;
public function __construct()
{
parent::__construct();
}
public function facebookInit()
{
$this->fb = new Facebook\Facebook([
'app_id' => 'APP_ID',
'app_secret' => 'APP_SECRET',
'default_graph_version' => 'v2.5',
]);
return $this->fb ;
}
//function to get login url for your app . Just create a controller which handles redirects after facebook login
public function loginUrl()
{
$helper = $this->facebookInit()->getRedirectLoginHelper();
$permissions = ['email','user_friends']; // Optional permissions
$loginUrl = $helper->getLoginUrl(site_url()."/FBController/", $permissions);
return $loginUrl ;
}
}
0
votes
Install php sdk v5 in codeigniter with composer https://www.sammyk.me/upgrading-the-facebook-php-sdk-from-v3-x-to-v5#installation-with-composer
create a controller and paste this code
$fb = new Facebook\Facebook([
'app_id' => 'app_id',
'app_secret' => 'app_secret',
'default_graph_version' => 'v2.4',
'default_access_token' => 'access_token'
]);
$response = $fb->get('/page_id');
$body = $response->getBody();
$body is an array, send it to your views for show your data.