开发者

is there proper codeigniter library to work with facebook php sdk

I have searched Google for Librari开发者_StackOverflowes for Facebook, and have found this one: http://www.haughin.com/code/facebook/, but it seems a bit outdated. I wanted something for this one: https://github.com/facebook/php-sdk/

I have written my own wrapper for it meanwhile, but it seems like I'm having some issues with $_REQUEST['...']


You can use Official PHP Facebook SDK in Codeigniter easily. The only problem is Facebook SDK needs $_REGISTER and in Codeigniter you don't have it.(Because of the mod_rewrite)

Here's the little solution:

1- This is for the $_REGISTER problem. Use this before loading Facebook class:

parse_str( $_SERVER['QUERY_STRING'], $_REQUEST );

2- Facebook SDK has 2 files. Put them into one file and save it to your application/helper folder as facebook_helper.php. Then loading and using is such an ease like this:

$this->load->helper( 'facebook' );
$facebook = new Facebook( array( 'appId' => 'XXX', 'secret' => 'xxx' ) );
// ... The rest is not different. You can read Facebook SDK examples


So here's a trick I used to use facebook PHP sdk with my CodeIgniter app. From the SDK code, take Facebook.php and take out the FacebookApiException class, and put it in a new file called FacebookApiException.php. Now, I put facebook.php and FacebookApiException.php into the models folder, and used them as regular models.

Here is the code I used for authenticating users and providing access to an application via Facebook.

function facebook_login(){
        # Creating the facebook object
        $facebook = new Facebook(array(
            'appId'  => 'XXXXXXXXXXXXXXXXX',
            'secret' => 'XXXXXXXXXXXXXXXXX',
            'cookie' => true
        ));

        # Let's see if we have an active session
        $session = $facebook->getSession();

        if(!empty($session)) {
            # Active session, let's try getting the user info
            try{
                $uid = $facebook->getUser();
                $param  =   array(
                   'method'  => 'users.getinfo',
                   'uids'       => $uid,
                   'fields'     => 'uid, username, name, profile_url, pic_big',
                   'callback'  => ''
                );
                $user = $facebook->api($param);
            } catch (Exception $e){
                $url = $facebook->getLoginUrl(array(  
                         'req_perms' => 'user_about_me, email, status_update, publish_stream, user_photos',
                         'next' =>  site_url()  . 'user/facebook_login',
                         'cancel' => site_url()
                     ));
                redirect($url);
            }

            if(!empty($user)){
                # User info ok? 
                print_r($user);

                    // Add user oauth token and info to DB, and then redirect to some controller in your application.
                    redirect('/'); // redirect to homepage

            } else {
                # For testing purposes, if there was an error, let's kill the script
                die("There was an error.");
            }
        } else {
            # There's no active session, let's generate one
            $url = $facebook->getLoginUrl(array(  
                         'req_perms' => 'user_about_me,email,status_update,publish_stream,user_photos',
                         'next' =>  site_url()  . 'user/facebook_login',
                         'cancel' => site_url()
                     ));
            redirect($url);
        }
    }

Hope this helps.


You can handle it in clear way by writing a small library by inheriting Facebook SDK class. Please follow my post on http://www.betterhelpworld.com/codeigniter/how-to-use-facebook-php-sdk-v-3-0-0-with-codeigniter to get it working.


There is also Facebook-Ignited.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜