Facebook feed using codeigniter

How to use FaceBook Php Sdk in CodeIgniter to call Facebook Graph Api and render your and your friends feed

In this tutorial you will learn how to use CodeIgniter Facebook library to fetch feeds using FaceBook graph Api and render on the browser.

First, you need to setup your codeigniter application with Facebook librThe coordinators. CodeIgniter does not come with Facebook library. Here is how to add the Facebook PHP SDK on the codeigniter project so that you can use the api like using other libraries.

  • Download FaceBook Php sdk from the github repository.
  • Copy three files (facebook.php, base_facebook.php and fb_ca_chain_bundle.crt files on your application libraries folder. Coping this on the libraries folder enables you to use facebook sdk easily.
  • Now create a controller to handle browser requests.
Controller class

Here we create myfacebook controller class to call Facebook api and use the json result to render back on the browser. Before calling api you need to setup you Facebook api with apiid and secret keys.

Here is the sample code for myfacebook controller class.

 class Myfacebook extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('facebook',array(
            'appId' => 'yourappid',
            'secret' => 'yoursecretkey',
        ));
    }
}

The facebook library has been loaded in the class construcer. Now we can use this library to make graph api calls. 

Here is the sample code to read your own feed and display on the browser. 

  public function  readstatus()
    {
        $userid=$this->facebook->getUser();

        try{
            $ret_obj= $this->facebook->api($userid.'?fields=posts','Get'
            );

            $this->renderposts($ret_obj);

        }
        catch(FacebookApiException $e)
        {
            $login_url = $this->facebook->getLoginUrl(array(
                'scope' => 'read_stream'
            ));
            echo 'Please <a href="' . $login_url . '">login.</a>';
            error_log($e->getType());
            error_log($e->getMessage());
        }
    }
  private function renderposts($facebookdata)
    {

        $posts= array();
        foreach($facebookdata['posts']['data'] as $status)
        {
            $profileimage='<img src="https://graph.facebook.com/' . $status['from']['id'] . '/picture">';
            $data= array('data'=>$status);
            $text=$this->load->view('posts/'. $status['type'],$data, true );

            $post= array('profileimage'=>$profileimage,'text'=>$text);

            $posts[]= $post;
        }
        $data['posts']=$posts;
        $this->load->view('header');
        $this->load->view('facebook_status',$data);

    }

Don't forget to add following views to your project. You should feel free to change the views.

view : facebook_status
 <div class="container">
    <div class="row-fluid">

        <div class="span3">

        </div>

        <div class="span6">
            <?php foreach ($posts as $data): ?>

                <div class="row">
                    <div class="span12 well" style="min-height: 100px;">
                        <div class="span2">
                            <?php
                            echo($data['profileimage']);
                            ?>
                        </div>
                        <div class="span10">

                            <?php echo($data['text']) ?>

                        </div>
                    </div>

                </div>

            <?php endforeach; ?>


        </div>
        <div class="span3">


        </div>
    </div>
</div>


</body>

</html>
view: status
 <?php
echo('<p>');
echo('<a href="' . $data['from']['id'] . '">');
echo($data['from']['name']);
echo('</a></br>');
if (isset($data['message'])) {
    echo($data['message']);
} else if (isset($data['story'])) {
    echo($data['story']);
}
echo('</p>');
?>

We will create a view file each for the post type. For eg: photo, video, etc. See how the view is loaded on the controller. $text=$this->load->view('posts/'. $status['type'],$data, true );

We are using different view for each post type because to simplify, handling different attributes associated with different post types.

view: link, photo, video
 <?php

echo('<a href="' . $data['from']['id'] . '">');
echo($data['from']['name']);
echo('</a></br>');
if (isset($data['story'])) {
    echo($data['story']);
}
echo('<a href="'. $data['link']. '">' );
echo('<div>');
echo(isset($data['name'])? $data['name']:null);
echo('<div class="span4">');
echo('<img src="' . $data['picture'] . '" />');
echo('</div>');
echo('<p>');
echo(isset($data['description'])?$data['description']:null);
echo('</p>');
echo('</div>');
echo('</a>');
?>