2

I am creating a custom helper in ./application/helpers however i am getting this error

Unable to load requested file helpers/curl_helper

This is the code in my helper file:

    function send(array $request, $url, $method)
{
    //Validating if the required extensions are installed or not
    if( !function_exists('json_encode') )   return false;
    if( !function_exists('curl_init') )     return false;

    //Converting the array into required json format
    $request = json_encode($request);

    //Setting header required for requests
    $header[] = "Content-type: application/json";
    $header[] = "Content-length: ".strlen($request) . "\r\n";

    //If the request method is get append the data into requests header
    if( $method == 'GET' or $method == 'get' )      $header[] = $request;

    //Initializing curl
    $ch = curl_init();
    //Setting curl options for request
    curl_setopt( $ch, CURLOPT_URL, $url);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, $header );
    curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $method );

    //If the request method is post add the data we need to enable post 
    //request and define the data in post fields
    if( $method == 'POST' or $method == 'post' ) {
        curl_setopt( $ch, CURLOPT_POST, 1);
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $request);
    }

    //Executing the curl request and storing the result in a variable
    $result = curl_exec( $ch );
    //Closing curl conneciton
    curl_close($ch);

    return json_decode($result);
}

And i am loading it in my library like:

$this->loader =& get_instance();
$this->loader->load->helper('curl');

Tell me where i am doing wrong?

UPDATE:
After trying too many things when i put the function in the same library where i want to use i found that there is an error in the line

curl_setopt( $ch, CURLOPT_HTTPHEADER, $header );

when i comment this line function works fine. I dont know where is the error please help me. And as far as i think this is the reason for loader error.

Code Prank
  • 4,209
  • 5
  • 31
  • 47

3 Answers3

4

Are you using CodeIgniter >= v 2.0.3? Inspecting the code igniter loader code, I can see only a couple of ways that helper loading fails:

  • File is not named correctly. your file must be have a .php extension ie. curl_helper.php but it seems you have already checked this.
  • File is not in the correct place (application/helpers). Again, it seems you have already checked this.
  • File is not accessible by the running web server. Permissions issues?
  • subclass prefix: What is your config setting for subclass prefix? This is the default setting:

    $config['subclass_prefix'] = 'MY_';

    CI allows users to override their default helpers by having them prefixed. For example you can override the array helper by having a file called MY_array_helper.php. If it happens to be that your helper matches the subclass prefix, then CI assumes that you are attempting to override a system helper and tries to make sure that the helper exists in the system helpers directory. For example, if you had a helper in application/helpers/MY_curl_helper.php then CI would check that there exists a helper in system/helper/curl_helper.php. In other words, make sure that the file of your helper does NOT match the subclass prefix.

RobertoP
  • 637
  • 3
  • 15
  • how are you calling the send() function ? One thing that I can see might be a problem is that when the function uses GET it appends a JSON string to $request, but that will result in invalid HTTP headers – RobertoP Mar 14 '12 at 18:04
2

Well, if you are using the "MY_helpername" then do check if you named the file on your server as "my_helpername", I don't know the reason but CI will look for my_helpername.php in helpers folder but not MY_helpername. I have this issue in past and changing the name on my linux box from MY_helpername to my_helpername worked for me.

Hope this helps

Junaid
  • 2,084
  • 1
  • 20
  • 30
  • yeah i have already done this don't know whether it is upto standards or not but i finally came to this solution. – Code Prank May 11 '12 at 09:35
-1

SOLUTION: For me, rename the file in lowercase, and works fine.