Search code examples
facebookcodeigniterpost

Posting on Facebook wall from Codeigniter app


I have a CI-based app that allows users to post an update stream similar to Facebook's wall.

Currently, users can authenticate into my app via Facebook using FB connect.

I would like to offer the possibility of a user -- when posting to my app's wall -- also be able to send the same post to his/her Facebook wall.

It seems clear the FB's Graph API support this but I'm having a hard time in finding a roadmap/ code/ library to help with this. The example on the above link is unhelpful and doesn't give me any idea how to implement this.

For example, how would a controller for this function look like?

I've found Elliot's FB CI library here, but am unsure if this is needed to accomplish what I want.

Any advice is greatly appreciated - thanks.


Solution

  • I ended up using a codeigniter helper function with curl to post to Facebook. Below is the code.

      function facebook($data) {
    
        $CI =& get_instance();
    
        $CI->load->model('fk_model');
    
        $token = $CI->fk_model->fk_cookie();
    
        $attachment =  array(
            'access_token'  => $token['access_token'],
            'message'       => $data['text'],
            'link'          => $data['link'],
        );
    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/' . $token['id'] . '/feed');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  //to suppress the curl output
        $result = curl_exec($ch);
        curl_close($ch);
        }