Posting on Facebook wall from Codeigniter app

只愿长相守 提交于 2019-12-22 14:08:56

问题


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.


回答1:


I would suggest you use Facebook PHP SDK

In your controller just include facebook php sdk eg: https://github.com/facebook/php-sdk/blob/master/examples/example.php

To make a wall post the following code should do the trick:

$wall_post = array('message' => 'this is my message',
                'name' => 'This is my demo Facebook application!',
                'caption' => "Caption of the Post",
                'link' => 'http://mylink.com',
                'description' => 'this is a description',
                'picture' => 'http://mysite.com/pic.gif',
                'actions' => array(array('name' => 'Get Search',
                                  'link' => 'http://www.google.com'))
                );    
$result = $facebook->api('/me/feed/', 'post', $wall_post);



回答2:


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);
    }   


来源:https://stackoverflow.com/questions/6336698/posting-on-facebook-wall-from-codeigniter-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!