my own Zend Facebook service

I am building a library in Zend Framework to make my web app easier to work with Facebook. So far I have modified on Zend_Oauth and create my own Oauth library so it fits with Facebook Oauth Scheme and it seems everything working well just I haven’t put down any comments yet because i still investigate what I have missed so far.. (Zend_Oauth Library seems to have more functionality which I have omitted some functions because they seem not fitting with facebook Oauth such as Post Header method which I think I can only work on Get method, but I am still investigating on it…)

In my own Oauth library, no request token is needed. First, user will be redirect to the Authorize URL with my App id and redirect URI, which really straight forward in my Oauth Consumer which just construct a URI and made a request. Then Facebook will redirect to whatever redirect URI is after authorized by user.

$zendConfig = new Zend_Config_Ini(APPLICATION_PATH.'/configs/application.ini', APPLICATION_ENV);

        $consumer = new SC_Oauth_Facebook_Consumer($zendConfig->facebook);
        $redirect = $consumer->redirect();

After redirected, I will pass $_GET parameters to my consumer (e.g. $consumer->getAccessToken($_GET)). Inside the consumer object, it has a Zend_Http_Client which will perform another Get method along with other parameters such as Client Secret and the ‘Code’ parameter which returned from Facebook. If everything is working, then an my object Access Token should be returned. In my example, I save it to the session.

  $zendConfig = new Zend_Config_Ini(APPLICATION_PATH.'/configs/application.ini', APPLICATION_ENV);

        $consumer = new SC_Oauth_Facebook_Consumer($zendConfig->facebook);

        $access_token = $consumer->getAccessToken($_GET);
    // My Session _ns
        $this->_ns->access_token = serialize($access_token);

Then I also write my own service class for Facebook(still working on it). In my test action,

    $zendConfig = new Zend_Config_Ini(APPLICATION_PATH.'/configs/application.ini', APPLICATION_ENV);

        $config = $zendConfig->facebook->toArray();
       
       
       if (isset($this->_ns->access_token)) {
            $access_token = unserialize($this->_ns->access_token);
        $config['accessToken'] = $access_token;
         $service = new SC_Service_Facebook($config);

        $this->view->user = $service->me();
       // I create a log to check those information on firebug instead of generating a view...
        $log = Zend_Registry::get('log');
       // $log->info($service->updateStatus('I am testing it~'));
      
      // a Like function to the status I just updated above
       $log->info($service->userLikes('684053594_182365461795153'));
       
        $log->info($service->userfeed());
        $log->info($service->userfriends());
        $log->info($service->userPosts());
        }

This is what have shown on Firebug on FireFox browser:

Forgot to mention my configuration files also add the following lines:

facebook.callbackUrl = YOUR_CALL_BACK_URL
facebook.clientId = YOUR_CLIENT_ID
facebook.clientSecret =YOUR_CLIENT_SECRET
facebook.scope = "publish_stream,offline_access,email" ; permission you want user to grant

So it seems working so far. 🙂 since I only draft it really quick and modified from Zend Framework Library.. I will need to clean them up little bit and comment them up when I have time… maybe I will post my code later in my other post or somewhere since there are many files…

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to MySpace Post to Ping.fm Post to Reddit Post to StumbleUpon

Leave a Reply