简体   繁体   中英

Codeigniter Helper function returning error

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.

A helper is a file with function, and not a class. But inside a helper you cannot load models and libraries as in the rest of Codeigniter. In order to do that, you need to create an instance of the main class,

This is your helper, functions_helper.php, located in application/helpers/:

   If ( ! defined('BASEPATH')) exit('No direct script access allowed');

   If ( ! function_exists('post_facebook_wall'))
   {
      function post_facebook_wall($post) {

        $CI =& get_instance();   //instantiate CodeIngiter main class
        //now you can call your models:
        $CI->load->model('facebook_model');

        $token = $CI->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);
    }

In your controllers or models or views you now have to LOAD your custom helper (or place it in the autoloader array in application/config/autoload.php $autoload['helper'] = array('functions_helper') ; ),

   $this->load->helper('functions_helper');

Now you can access your funcions the usual way,

$this->functions_helper->post_facebook_wall($post)

you can't reference $this inside a function (read about OOP)

use get_instance() to grab a reference to the controller object, something like:

$obj = get_instance();
$obj->load->model('facebook_model');
// etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM