Welcome Guest, Not a member yet? Register   Sign In
how to include facebook sdk initial?
#1

(This post was last modified: 01-15-2017, 07:43 PM by BHunter.)


Hi I'm beginner in Codeigniter. I have 4 view page that have facebook login (1. login page 2.register page 3.login special member page 4.register special member page)
And all 4 views file I have to initial facebook php sdk like this

Code:
<?php require_once(APPPATH.'libraries/facebook/src/facebook.php');
$facebook = new Facebook(array(
 'appId'  => 'xxxxxxxxxxxxxx',
 'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
));


$user = $facebook->getUser();

It's very annoying that one day I have to change appID and appsecret, I have to change it in 4 view files.
so I create new view file name "fb_init.php" that contain code

Code:
<?php require_once(APPPATH.'libraries/facebook/src/facebook.php');
   $facebook = new Facebook(array(
     'appId'  => 'xxxxxxxxxxxxxxxx',
     'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
   ));


   $user = $facebook->getUser();

and then in 4 view files I include fb_init.php by write

Code:
$this->load->view('fb_init.php');

However, The problem is the remaining code of facebook php sdk cannot refer to 

Code:
$user

 variable. the remaining code of facebook sdk is here.

Code:
   if ($user) {
     try {
       // Proceed knowing you have a logged in user who's authenticated.
       $user_profile = $facebook->api('/me?fields=id,email,name,first_name,last_name,picture'); //ต้องมีหลัง /me ไม่งั้นประวัติมาไม่ครบ
     } catch (FacebookApiException $e) {
       error_log($e);
       $user = null;
     }
   }

$loginUrl = $facebook->getLoginUrl(array(  
   "redirect_uri"=>"http://www.club55.net/login",  
   "scope"=>"email"
 ));

How could I do?
Reply
#2

(This post was last modified: 01-15-2017, 09:43 AM by PaulD. Edit Reason: Added PS )

You need a library. Then return the $user.

Create a file in libraries called Facebook_library.php

In it do something 'like' this:

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

class 
Facebook_library {
 
    
     
protected $appId '201637766943985';
 
    protected $secret '4e70dc1dbfc4787e81bec0d7b57d6a1c';
 
    
     
public function __construct()
 
    {
 
         // only needed to access CI objects
 
         // like $CI->load->library('form_validation');
 
         $this->CI =& get_instance();
 
         
          require_once
(APPPATH.'libraries/facebook/src/facebook.php');
 
    }
 
    
     
public function get_user($user_id$type='') {
 
         $facebook = new Facebook(array(
 
              'appId'  => $this->appId,
 
              'secret' => $this->secret,
 
         ));
 
         $user $facebook->getUser();
 
         
          return $user
;
 
    }


Then in your controller or any controller or any time you need it:

PHP Code:
$this->load->library('facebook_library');
$user $this->facebook_library->get_user(); 

I have just typed this out and not done it anywhere so it is probably full of bugs as I typed it. But you can read about libraries here: https://www.codeigniter.com/user_guide/g...aries.html

They are extraordinarily useful. And you can keep your code very DRY. And as with any class you can have many functions like delete_user, authorise_user, get_user_profile or whatever. And if your FB details change you only need to change them in one place. In fact, you could put them into the config file and call them as config items in the library.

Alternatively you can do similar in a model, and then you do not need to worry about the CI super object.

Hope that helps in some way,

Best wishes,

Paul.

Edit: PS There are lots of resources, advice, help files and example codes if you just google for something like "codeigniter facebook sdk library"
Reply
#3

Particularly bad idea to post a Facebook app key on a public forum. Just sayin' Undecided
Reply
#4

I presumed he had just done some random representative numbers. But if they are genuine, not a good idea at all.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB