Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6029929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:57:38+00:00 2026-05-23T04:57:38+00:00

I would like to use the code below as a helper function, since it

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:57:39+00:00Added an answer on May 23, 2026 at 4:57 am

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the below code, I would like to use the code in static load
I would like to use a spinner. But, this code below does not display
I have a few lines of PowerShell code that I would like to use
Considering the code below. I would like to run 3 experiments at a time.
I would like to use thread to process a streaming input. How can make
I would like to use the new Parallel.ForEach function to loop through a datatable
I would like to use a language that I am familiar with - Java,
I would like to use something like CLR Profiles on .Net 2.0 to see
I would like to use as and is as members of an enumeration. I
I would like to use a component that exposes the datasource property, but instead

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.