Posting on Facebook wall from Codeigniter app

旧城冷巷雨未停 提交于 2019-12-06 13:25:47

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

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