Elliot Haughin

I'm a freelance web developer.
I build clever, engaging, ass-kicking,
web applications. You want me?

This library is essentially a wrapper for the Facebook PHP API, but also includes everything you need to get Facebook Connect working with in your CodeIgniter libraries.

This is an early (beta)-ish release, so the documentation isn’t too great, just wanted to get this out in the wild.

Overview
=========================
This package includes everything you need to get your application using both the server-side facebook api and client-side facebook connect functions.

You can read more about this at http://www.haughin.com/code/facebook/

Licensing
=========================
facebook-client directory and contents licensed by Facebook.
All other contents licensed under GNU/GPL (see license.txt)

Basic setup instructions
=========================
1. Copy the files within application/ to your CodeIgniter application directory.
Included is one config file (facebook.php), one library file (facebook_connect.php), the facebook-client
one sample controller (home.php), and one sample view file (fbtest.php).

2. Copy xd_receiver.htm to the root of your CodeIgniter install (in the same directory as CI’s index.php)

3. Go to facebook developer application and register your application ( http://www.facebook.com/developers/ )

4. Copy your API and Secret key to config/facebook.php

5. In the facebook developer application, set your ‘Connect URL’ under the connect tab, to the CodeIgniter application url.

I.E. http://mygroovyapp.com/

6. If your application lives on a subdomain (like : http://my.groovyapp.com ) set your ‘base domain’ under the connect tab to the top level domain of your application ( in this case: groovyapp.com )

7. Give it a go!

Coming Soon:

  • Better Documentation
  • Installation Screencast
  • ‘Live’ running demo application

Download Facebook Connect CodeIgnter Library

License: GNU GENERAL PUBLIC LICENSE – Version 2
Version: 1.0

Sponsored Links

Comments

  1. Andy says:

    This looks cool, look forward to trying it. There isn’t many APIs you haven’t covered lol

  2. obaid says:

    I had written my own wrapper around it, but this should really help a lot.. !

    thanks

  3. [...] save some time. Thankfully Elliot Haughin has done just that for CodeIgniter developers. Last week Elliot released an extremely useful Facebook Connect CodeIgniter library that will get you up and running in under [...]

  4. aldi says:

    beautiful! I almost create it myself. Thank you. Gonna try this :)

  5. [...] One downside of this feature is that it’s not directly integrated into CodeIgniter’s session handling library but a few tweaks could integrate it pretty easily. This library will most definitely come in handy. The version released by Elliot is extremely simple and it works. If you are also implementing Facebook Connect within a CodeIgniter application, have a look here Ellion Haughin’s library. [...]

  6. [...] Facebook Connect CodeIgniter Library [...]

  7. [...] Elliot Haughin – Facebook Connect CodeIgniter Library [...]

  8. Very clean, not any duplication of stuff already built into their rest client. There’s an updated rest client on their site that contains the Open Stream API stuff as well if you want to update your source. You could also consider hosting it on Github so other folks could update that stuff for you :-)

  9. [...] Elliot Haughin – Facebook Connect CodeIgniter Library Compartelo :codeigniter, Facebook, Framework, Php [...]

  10. [...] Facebook Connect CodeIgniter Library [...]

  11. Charles says:

    I been getting an error after a few days of logging in


    Fatal error: Uncaught exception 'FacebookRestClientException' with message 'Session key invalid or no longer valid' in /home/eatmyswan/appuseful.com/application/libraries/facebook-client/facebookapi_php5_restlib.php:1915 Stack trace: #0 /home/eatmyswan/appuseful.com/application/libraries/facebook-client/facebookapi_php5_restlib.php(786): FacebookRestClient->call_method('facebook.users....', Array) #1 /home/eatmyswan/appuseful.com/application/libraries/facebook_connect.php(57): FacebookRestClient->users_getInfo('7926111', Array) #2 /home/eatmyswan/appuseful.com/application/libraries/facebook_connect.php(42): Facebook_connect->_manage_session() #3 /home/eatmyswan/appuseful.com/system/libraries/Loader.php(931): Facebook_connect->Facebook_connect() #4 /home/eatmyswan/appuseful.com/system/libraries/Loader.php(829): CI_Loader->_ci_init_class('facebook_connec...', '', NULL, NULL) #5 /home/eatmyswan/appuseful.com/system/libraries/Loader.php(96): CI_Loader->_ci_load_class('facebook_connec...', NULL, NULL) #6 /home/eatmyswan/appuse in /home/eatmyswan/appuseful.com/application/libraries/facebook-client/facebookapi_php5_restlib.php on line 1915

  12. Charles says:

    I been getting an error after a few days of logging in

    Fatal error: Uncaught exception ‘FacebookRestClientException’ with message ‘Session key invalid or no longer valid’ in /home/eatmyswan/appuseful.com/application/libraries/facebook-client/facebookapi_php5_restlib.php:1915 Stack trace: #0 /home/eatmyswan/appuseful.com/application/libraries/facebook-client/facebookapi_php5_restlib.php(786): FacebookRestClient->call_method(’facebook.users….’, Array) #1 /home/eatmyswan/appuseful.com/application/libraries/facebook_connect.php(57): FacebookRestClient->users_getInfo(’7926111′, Array) #2 /home/eatmyswan/appuseful.com/application/libraries/facebook_connect.php(42): Facebook_connect->_manage_session() #3 /home/eatmyswan/appuseful.com/system/libraries/Loader.php(931): Facebook_connect->Facebook_connect() #4 /home/eatmyswan/appuseful.com/system/libraries/Loader.php(829): CI_Loader->_ci_init_class(’facebook_connec…’, ”, NULL, NULL) #5 /home/eatmyswan/appuseful.com/system/libraries/Loader.php(96): CI_Loader->_ci_load_class(’facebook_connec…’, NULL, NULL) #6 /home/eatmyswan/appuse in /home/eatmyswan/appuseful.com/application/libraries/facebook-client/facebookapi_php5_restlib.php on line 1915

  13. I’ve been contacted to implement Facebook and Twitter into a new project. As I was planning to work with CI anyway, this (and your Twitter library) will undoubtebly help me a lot! Thanks a bunch.

    I still need to take a look at the actual code, but none the less, is there a screencast coming?

  14. Dan Morin says:

    I’m just curious why you chose the GPL for the license as it obviously limits who can use the code. Imagine what we couldn’t do if CI was GPL. Anyway, not trying to undervalue your contributions, I was just curious.

  15. Jeffrey says:

    Elliot, if I’m not mistaken, you can’t test this against localhost, is that correct?

    If so it might be worth mentioning this in the documentation.

  16. jeff says:

    bug report!!
    if you are trying to login with other user while you previous login and logout with other username. the current library will not destory previous session, and it will keep previous login info and over write the current login

    what I did

    in facebook_connect.php

    form
    if ( $user === FALSE && $this->user_id !== NULL )

    change to

    if ( $user !== NULL && $this->user_id !== NULL )

  17. Aniket says:

    Elliot, the library is working with php5 but not with php 4 . any suggestions for making it work with php 4 ?

  18. Nice library, thanks for the good work.

  19. Kevin says:

    Thanks Elliot awesome job!! After a ton of research and frustration I finally have a codeigniter application running in facebook. I tried this tute but still had a few problems… so I decided to do something about it. Once I got it running I zipped it to share with other CI/Facebook noobs like me.

    This is the complete CodeIgniter code with Elliots Facebook Connect library built in. Download the zip file and check out the install tutorial at viducation.info to get a new facebook app ready in about 10 minutes.

    http://www.crashwrestling.com/viducation/index.php?option=com_docman&task=doc_download&gid=6&Itemid=83

  20. Aljaz says:

    When I try to use photos_get function I get the following error:

    Fatal error: Uncaught exception ‘FacebookRestClientException’ with message ‘A session key is required for calling this method’ in /home/public_html/testt/system/application/libraries/facebook-client/facebookapi_php5_restlib.php:1915

    Any idea whys that?

  21. Eddie says:

    For those getting this error:

    Fatal error: Uncaught exception ‘FacebookRestClientException’ with message ‘Session key invalid or no longer valid’

    I was able to fix it adding “try & catch” to the facebook_connect.php inside the library.

    You can copy and past this code if you like.

    if ( $user === FALSE && $this->user_id !== NULL )
    {

    try {
    $profile_data = array(’uid’,'first_name’, ‘last_name’, ‘name’, ‘locale’, ‘pic_square’, ‘profile_url’);
    $info = $this->fb->api_client->users_getInfo($this->user_id, $profile_data);

    $user = $info[0];

    $this->_obj->session->set_userdata(’facebook_user’, $user);
    } catch(Exception $e) {
    // Do nothing.. or send you an email or something
    }

    }

    I hope this helps!

  22. [...] – “offline_access” and “status_update”. This is also using Elliot Haughin’s Facebook plugin for CodeIgniter. Elliot’s package includes an older version of the Facebook PHP Library, so I [...]

  23. Chris says:

    Just wanted to say a big THANKS! for all that you have contributed.
    I’m starting a new project and the libraries/code you have supplied in this article, along with the flickr and twitter articles are going to give me a good starting point and save me alot of time.

  24. Musa TAlukdar says:

    Thanks man. demo is working fine.

  25. shafiq says:

    gr8 work !! well done :)

  26. Pjerky says:

    For those getting ‘Session key invalid or no longer valid’ errors, I ran into this problem too when I created my own Facebook Connect tie-in for a project at work.

    I dug around for awhile and eventually found out that there is a flaw in the session retrieval code that Facebook provides that makes it not grab the correct information. But there is an easy workaround for it.

    The trick is to pull the correct session key yourself. The key is stored in a cookie that your domain can access. The name of this cookie is always your api key with ‘_session_key’ appended to the end.

    So in the constructor for the facebook library I did the following BEFORE calling the Facebook Rest Client.

    //Lets get the session key really quickly
    $cookie_name = $api_key . ‘_session_key’;

    if (isset($_COOKIE[$cookie_name]) && !empty($_COOKIE[$cookie_name]))
    $session_key = $_COOKIE[$cookie_name];
    else
    $session_key = NULL;

    $this->api_client = new FacebookRestClient($api_key, $secret, $session_key);

    I hope this helps, it brought the instances of this error down to zero for me.

    Thanks,
    Pjerky

  27. K says:

    Hi, guys!
    could you please run onto the session issue once again?

    here is the setting:
    1. Sessions lib is started by autorun long before facebook lib is initiated.
    2. Once upon the time (I think it relates to session expiration time) session lib can’t find session data in DB. Let’s say it searches for `session_id` = ‘45c3fdbfa5f7f0b7fee2c0f436f15bc7′
    3. the facebook lib creates a new session record in DB. The new session_id is different from the id in the 2nd line.
    4. On the next page load the cycle 1 through 4 repeats. It causes a problem if user has to pass an intro screen every time she logs in. The user is effectively trapped on the into screen. Second problem occurs because DB is populated with useless sessions, since a session per page load is created.

    Do you have an idea how to resolve it?

    here is additional info:
    A. A copy of `session_id` = ‘45c3fdbfa5f7f0b7fee2c0f436f15bc7′ into the DB field ’session_id’ over one of the onetimers session ids helps for a while. I guess until the session expires.
    B. `session_id` = ‘45c3fdbfa5f7f0b7fee2c0f436f15bc7′ is just an example and changes after a period of time.
    C. the actual code is:

    class Facebook_connect {
    private $_obj;
    private $_api_key = NULL;
    private $_secret_key = NULL;
    public $user = NULL;
    public $user_id = FALSE;
    public $fb;
    public $client;

    function Facebook_connect()
    {
    log_message(’debug’, ‘Facebook: facebook connection initialized.’);
    $this->_obj =& get_instance();

    $this->_obj->load->config(’facebook’);

    $this->_api_key = $this->_obj->config->item(’facebook_api_key’);
    $this->_secret_key = $this->_obj->config->item(’facebook_secret_key’);
    $this->app_callback_url = $this->_obj->config->item(’facebook_app_callback_url’);

    log_message(’debug’, ‘Facebook: starting instance…’);

    $this->fb = new Facebook($this->_api_key, $this->_secret_key);
    $this->client = $this->fb->api_client;

    #require authentication
    //$type = ‘iFrame’;
    $type = ‘fbApp’;
    if($type == ‘fbApp’) {
    $this->fb->require_frame();
    $this->user_id = $this->fb->require_login();
    }
    else{
    $this->user_id = $this->fb->get_loggedin_user();
    }

    $this->_manage_session();
    log_message(’debug’, ‘Facebook: …instance done.’);
    }

    private function _manage_session(){
    log_message(’debug’, ‘Facebook: facebook session management initialized.’);
    $user = $this->_obj->session->userdata(’facebook_user’);

    //if ( $user === FALSE && $this->user_id !== NULL )
    if ( $user !== NULL && $this->user_id !== NULL )
    {
    log_message(’debug’, ‘Facebook: Manage Session: try session recovery.’);
    $profile_data = array(’uid’,'first_name’, ‘last_name’, ‘name’, ‘locale’, ‘pic_square’, ‘profile_url’);
    $info = $this->fb->api_client->users_getInfo($this->user_id, $profile_data);
    $user = $info[0];

    $this->_obj->session->set_userdata(’facebook_user’, $user);
    log_message(’debug’, ‘Facebook: Manage Session: session recovery – DONE.’);
    // create session v2
    //$user = $this->inquire_user_profile($this->user_id);
    //log_message(’debug’, ‘Facebook: Manage Session: session is created.’);
    //$this->_obj->session->set_userdata(’facebook_user’, $user);
    }
    elseif ( $user !== FALSE && $this->user_id === NULL )
    {
    // Need to destroy session
    $this->_obj->session->unset_userdata(’facebook_user’);
    log_message(’debug’, ‘Facebook: Manage Session: data destroyed.’);
    }

    if ( $user !== FALSE )
    {
    // check if updated data is available
    $tmp = $this->fb->api_client->users_getInfo($this->user_id, ‘ profile_update_time’);
    $tmp = $tmp[0]['profile_update_time'];
    if ($tmp > (isset($user['profile_update_time'])?$user['profile_update_time']:0) ){
    $user = $this->inquire_user_profile($this->user_id);
    $this->_obj->session->set_userdata(’facebook_user’, $user);
    log_message(’debug’, ‘Facebook: Manage Session: session is updated with facebook data.’);
    }
    // pass user data to app
    $this->user = $user;
    }
    }

    Thank you in advance!

  28. Rusty Cage says:

    Elliot;

    You are a Godsend – it’s such an elegant and self-documenting class wrapper. Really doing everyone a big favor.

    Plus, it’s so easy to tweak it for a different PHP framework (like Symfony for instance).

    Thank you very much for clarifying how to make Facebook Connect work in a neat and discrete fashion.

  29. Farid says:

    For the ‘Session key invalid or no longer valid’ problem

    I replaced :

    $this->user_id = $this->fb->get_loggedin_user();

    with :

    if($this->fb->get_loggedin_user()) {
    try {
    $this->user_id = $this->fb->api_client->fql_query(’SELECT uid, pic_square, first_name FROM user WHERE uid = ‘ . $this->fb->get_loggedin_user());
    }
    catch (Exception $ex) { }
    }

  30. Farid says:

    If you get “An Error Was Encountered The URI you submitted has disallowed characters.”

    You can set $config['uri_protocol'] to “PATH_INFO” in config.php

  31. Darrin says:

    I’m just getting a straight-up 500 error using the default install.

    Any ideas what’s going on there anyone?

  32. othman says:

    Well done Elliot !
    Thank you very much.

  33. Roger says:

    Darrin,

    Most likely, you’re running into a .htaccess issue.

    Thanks for the great Library Elliot!

  34. Tilo says:

    Has anyone been able to resolve the issue with the sessions? I still get the message:

    …Uncaught exception ‘FacebookRestClientException’ with message ‘Session key invalid or no longer valid’…

    I have tried the solutions proposed by Farid, Pjerky, Eddie and Jeff, but to no avail.

    Plz post if you have been able to solve.

    Cheers,
    Tilo

  35. andri says:

    i got error like this

    Severity: Warning

    Message: CI_Loader::include_once(application/front/libraries/facebook_connect.php) [function.CI-Loader-include-once]: failed to open stream: No such file or directory

    Filename: libraries/Loader.php

    Line Number: 827

    An Error Was Encountered

    Non-existent class: facebook_connect

    does anybody know where to fix?

    thanks

Leave a Comment

© Copyright 2010 Elliot Haughin