I would like to use the code below as a helper function, since it will be called from several controllers in my app.
As you can see this is a PHP curl to post text to a user's Facebook wall.
} elseif ($this->input->post('post_facebook')) { //"post to facebook" checkbox is ticked
$this->load->model('facebook_model');
$token = $this->facebook_model->get_facebook_cookie();
$attachment = array(
'access_token' => $token['access_token'],
'message' => $post['posts_text'],
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/' . $token['uid'] . '/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);
This code works perfectly when inside a controller. But I would like to do something like this instead:
Put this in helpers/functions_helper.php:
function post_facebook_wall($post) {
$this->load->model('facebook_model');
$token = $this->facebook_model->get_facebook_cookie();
$attachment = array(
'access_token' => $token['access_token'],
'message' => $post['posts_text'],
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/' . $token['uid'] . '/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); }
Then in my controller:
...
} elseif ($this->input->post('post_facebook')) { //post to facebook checkbox is ticked
$post = array('posts_text' => 'sample text');
post_facebook_wall($post);
}
Unfortunately this isn't working, and I get a 500 error.
functions_helper.php is in autoload.
Any ideas what I'm doing wrong here? Thanks in advance.