Welcome Guest, Not a member yet? Register   Sign In
how to include facebook sdk initial?
#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


Messages In This Thread
how to include facebook sdk initial? - by BHunter - 01-15-2017, 02:29 AM
RE: how to include facebook sdk initial? - by PaulD - 01-15-2017, 09:39 AM



Theme © iAndrew 2016 - Forum software by © MyBB