Welcome Guest, Not a member yet? Register   Sign In
Facebook Controller
#1

[eluser]Fatih[/eluser]
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.

Code:
<?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:

Code:
<?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 :

Code:
<?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.

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

.name {
  padding-top: 0px;
  text-align: center;
}

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


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

This is my way Smile
#2

[eluser]Fatih[/eluser]
Here is the Turkish translation of this post, if someone is interested.
#3

[eluser]florachan[/eluser]
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
#4

[eluser]Fatih[/eluser]
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.
#5

[eluser]florachan[/eluser]
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 ?.
#6

[eluser]Fatih[/eluser]
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.
#7

[eluser]florachan[/eluser]
[quote author="fatigue" date="1248346036"]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.[/quote]

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

[eluser]Fatih[/eluser]
Sorry, I didn't get it same error. Maybe better for you if you search "disallowed characters" in this forum.
#9

[eluser]searain[/eluser]
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?
#10

[eluser]Fatih[/eluser]
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.




Theme © iAndrew 2016 - Forum software by © MyBB