Part of the EllisLab Network
   
1 of 2
1
Facebook Controller
Posted: 17 June 2009 03:52 AM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  73
Joined  12-12-2007

Facebook is getting bigger and more of us want to code with API of Facebook. If you integrate with CodeIgniter and Facebook, you have two choice: one of them is Junal’s approach and another way is Elliot’s way. My approach is quite different then.

Firstly, I create a config file which name is facebook.php in application/config directory as coded below. You shall be changed these sentences as your case.

<?php

    $config[
'facebook_api_key''your_api_key';
    
$config['facebook_secret_key''your_secret_key'

Secondly, I downloaded Facebook client library form developer center. These files shall be stored on application/plugins directory (if there is not folder named plugins then you must create it) and renamed my facebook.php file to facebook_pi.php.

These steps are almost same with Junal’s way. After that I create MY_Controller.php file on application/library directory as coded below:

<?php if (!defined('BASEPATH')) exit('No direct access allowed.');

class 
Facebook_Controller extends Controller {
    
public $facebook;
    public 
$user;

    function 
Facebook_Controller() 
    
{
        parent
::Controller();
        
        
$this->load->config('facebook');
            
        
$this->__fbApiKey    $this->config->item('facebook_api_key');
        
$this->__fbSecret    $this->config->item('facebook_secret_key');
        
        
$this->load->plugin('facebook');
          
// Prevent the 'Undefined index: facebook_config' notice from being thrown.
        
$GLOBALS['facebook_config']['debug'NULL;
          
// Create a Facebook client API object.
        
$this->facebook = new Facebook($this->__fbApiKey$this->__fbSecret);
        
$this->user $this->facebook->require_login();
    
}

Then, create a default controller file which name is welcome.php on application/controller directory :

<?php

class Welcome extends Facebook_Controller {

    
function Welcome ()
    
{
        parent
::Facebook_Controller();    
    
}

    
function index()
    
{
       
// Retrieve the user's friends and pass them to the view.
       
$data['friends'$this->facebook->api_client->friends_get();
       
$this->load->view('welcome_message' ,$data);
    
}

Finally, here is my welcome_message.php file which is stored on application/view directory.

<style type="text/css">
 .
img {
  float
left;
  
padding 10px 0px 0px 10px;
  
width:50px;
  
overflowvisible;
}

.name {
  padding
-top0px;
  
text-aligncenter;
}

.container
{
  padding
-left:18px;
}
</style>


<
div class="container">
<?php
foreach ($friends as $id)
{
?>
<div class='img'>
    <
fb:profile-pic uid='<?=$id?>' firstnameonly 'true' linked='true' size='square'/>
<
div class='name'>
        <
fb:name uid='<?=$id?>' firstnameonly 'true' capitalize='true'  useyou 'false'/>
    </
fb></div>
</
fb></div>
<?php
}
?>
</div

This is my way smile

 Signature 

audentis fortuna iuvat
CodeIgniter Turkey

Profile
 
 
Posted: 17 June 2009 03:05 PM   [ Ignore ]   [ # 1 ]  
Grad Student
Avatar
Rank
Total Posts:  73
Joined  12-12-2007

Here is the Turkish translation of this post, if someone is interested.

 Signature 

audentis fortuna iuvat
CodeIgniter Turkey

Profile
 
 
Posted: 22 July 2009 10:02 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  4
Joined  07-22-2009

Hi any one know if this work with the latest code igniter ? .. I tried it but it gave me the following error


An Error Was Encountered

The URI you submitted has disallowed characters.

please help , thanks

Profile
 
 
Posted: 22 July 2009 10:14 AM   [ Ignore ]   [ # 3 ]  
Grad Student
Avatar
Rank
Total Posts:  73
Joined  12-12-2007

Hello florachan,

Welcome to CodeIgniter forum pages first of all. This controller is working. This error message is not relevant with this controller. You must check your $config[‘permitted_uri_chars’] variable in application/config.php file.

 Signature 

audentis fortuna iuvat
CodeIgniter Turkey

Profile
 
 
Posted: 22 July 2009 05:16 PM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  4
Joined  07-22-2009

Thanks for the fast reply, what character I need to allow in order for it to accept facebook call back ?. I did tried let $config[‘permitted_uri_chars’] allow all char but it said not found . any tips on get me going ? or do you have a working example that I can download so that I can compare what I did wrong ?.

Profile
 
 
Posted: 23 July 2009 01:47 AM   [ Ignore ]   [ # 5 ]  
Grad Student
Avatar
Rank
Total Posts:  73
Joined  12-12-2007

It is very dangerous to allow all characters in $config[‘permitted_uri_chars’]. I guess you shall read more about CodeIgniter before you use facebook controller.

 Signature 

audentis fortuna iuvat
CodeIgniter Turkey

Profile
 
 
Posted: 23 July 2009 08:34 AM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  4
Joined  07-22-2009
fatigue - 23 July 2009 05:47 AM

It is very dangerous to allow all characters in $config[‘permitted_uri_chars’]. I guess you shall read more about CodeIgniter before you use facebook controller.

it is just me or did you encounter the same problem ? how did you solve the not permitted uri problem ?

Profile
 
 
Posted: 23 July 2009 08:59 AM   [ Ignore ]   [ # 7 ]  
Grad Student
Avatar
Rank
Total Posts:  73
Joined  12-12-2007

Sorry, I didn’t get it same error. Maybe better for you if you search “disallowed characters” in this forum.

 Signature 

audentis fortuna iuvat
CodeIgniter Turkey

Profile
 
 
Posted: 26 July 2009 12:31 PM   [ Ignore ]   [ # 8 ]  
Lab Assistant
RankRank
Total Posts:  179
Joined  06-03-2009

Could you tell a little bit more about what is the different among your approach and other two approaches?  Maybe it will help me to pick up one to try first (instead of I trying all 3 and pick up one).  There must be a good reason you built this rather than using the other two approaches.

By the way, what is the “license” of your script?

Profile
 
 
Posted: 26 July 2009 02:49 PM   [ Ignore ]   [ # 9 ]  
Grad Student
Avatar
Rank
Total Posts:  73
Joined  12-12-2007

Well. In Junal approach, you must setup on every controller files (define Facebook api keys, secret keys, load Facebook plugin etc). Same codes were wrote on every controller files.  If you can change your setup, you must edit all files, also.

In my way, Facebook constants are in separated config file (like as Elliot’s way) and created MY_Controller file (This is different Elliot’s way). If you use Facebook class, you must just indicated on top of controller file (like as “class Welcome extends Facebook_Controller”). That’s it.

This is a post to show about CodeIgniter’s different solutions. Therefore, you can use my codes without any permission.

 Signature 

audentis fortuna iuvat
CodeIgniter Turkey

Profile
 
 
Posted: 26 July 2009 04:48 PM   [ Ignore ]   [ # 10 ]  
Lab Assistant
RankRank
Total Posts:  179
Joined  06-03-2009
fatigue - 23 July 2009 12:59 PM

Sorry, I didn’t get it same error. Maybe better for you if you search “disallowed characters” in this forum.

He needs to set up his application canvas mode as FBML instead of default IFrame.

Profile
 
 
Posted: 26 July 2009 04:50 PM   [ Ignore ]   [ # 11 ]  
Lab Assistant
RankRank
Total Posts:  179
Joined  06-03-2009
function index()
    
{
       
// Retrieve the user's friends and pass them to the view.
       
$data['friends'$this->facebook->api_client->friends_get();
       
$this->load->view('welcome_message' ,$data);
    

You try to load the view page ‘welcome_message’.  But then you saved your view page as ‘welcome_view’.

Should it be $this->load->view(‘welcome_view’ ,$data)?

Profile
 
 
Posted: 26 July 2009 05:02 PM   [ Ignore ]   [ # 12 ]  
Lab Assistant
RankRank
Total Posts:  179
Joined  06-03-2009

Thanks a lot!

I got your sample codes working.

Could you also share any resource (web sites, books, social media resource, face book open source systems etc.) you have used and you are using about face book application?  Not limited to but specially Codeigniter and face book application development?

Face book said there are 1 million face book developers, I would assume large part of them are php developers.

Not so many good online resources available though.  Yours is one of the best.  But I only found start tutorial like this.  Not too many comprehensive guides available.

Profile
 
 
Posted: 27 July 2009 01:27 AM   [ Ignore ]   [ # 13 ]  
Lab Assistant
RankRank
Total Posts:  179
Joined  06-03-2009

By the way.

Did you ever work on google map api in your face book application?  specially based on this sample code?

For example,

<fb:iframe src=“http://facebook.foliosystems.ca/facebook/google_map.php” smartsize=true></fb:iframe>

if the src=’...’ is a traditional php site, it is fine.  but if the src is CI site, then I get the error message of “not found the page”. 

it seems the iframe src CI site and conflict with the facebook application CI site.

Profile
 
 
Posted: 27 July 2009 01:36 AM   [ Ignore ]   [ # 14 ]  
Lab Assistant
RankRank
Total Posts:  179
Joined  06-03-2009

Information for people using the same sample codes that may have the same problem.  I found this thread to solve the above problem of <fb:iframe src=’...’

http://www.simpleprojectz.com/2008/10/facebook-codeigniter/

Instead set up

$config[‘uri_protocol’] = “PATH_INFO”;

like this thread said, I need to set

$config[‘uri_protocol’]  = “ORIG_PATH_INFO”;

It works fine now.

But I am so sure what is the logic here.

In simple words

$config[‘uri_protocol’]  = “ORIG_PATH_INFO”;

to make the fb iframe work, why?

Profile
 
 
Posted: 27 July 2009 04:22 AM   [ Ignore ]   [ # 15 ]  
Grad Student
Avatar
Rank
Total Posts:  73
Joined  12-12-2007
blackhorse66 - 26 July 2009 08:50 PM

You try to load the view page ‘welcome_message’.  But then you saved your view page as ‘welcome_view’.

Should it be $this->load->view(‘welcome_view’ ,$data)?

Yes, you are correct. I edited my codes, and revised view file name as welcome_message.php.

 Signature 

audentis fortuna iuvat
CodeIgniter Turkey

Profile
 
 
   
1 of 2
1