1

I have tried all possible solutions on Stack as well as followed the creating library guide on CI user guide please let tell me what im doing wrong.

Im trying to connect an api call library Convioapi with my login class see below. I have tried loading the library in the construct

I still keep getting the same error:

An Error Was Encountered

Unable to load the requested class: Convioapi

class Convioapi
{
    //public assigned variables
    public $host = 'qwerqwerqwerqwer';
    public $short_name = 'qwerqwerqwerqw';
    public $api_key = 'qwerqwerqwerqwerwe';
    public $v = '123412341';
    public $response_format = 'json';

    //private variables
    private $__method;
    private $__methodParams = array();
    private $__servlet;

    public function __construct($data = array())
    {
        $this->CI =& get_instance();

        if (count($data) > 0 ){
            foreach ($data as $key )
            {
                $login_name = $data['username'];
                $login_password = $data['password']; 
                $servletMethod = $data['servletMethod'];
                }    
            }else{
                $login_name = NULL;
                $login_password = NULL;
                $servletMethod;
            }


        $this->call($servletMethod);
    }

    private function __getPostData()
    {
        $response_format = $this->response_format;
        if ($this->response_format == 'php') $response_format = 'json';
        $baseData   = http_build_query(array('v'=>$this->v,'api_key'=>$this->api_key,'response_format'=>$response_format,'login_name'=>$this->login_name,'login_password'=>$this->login_password,'method'=>$this->__method));
        $methodData = http_build_query($this->__methodParams);
        return sprintf('%s&%s', $baseData, $methodData);
    }

    private function __makeCall()
    {
        $url  = $this->__getUrl();
        $post = $this->__getPostData();

        // Here is where we check for cURL. If we don't find it we make a fopen call...
        if (function_exists('curl_exec') === FALSE)
        {
            $context = stream_context_create(array('http'=>array('method'=>'POST','content'=>$post)));
            $fp = @fopen($url, 'rb', FALSE, $context);
            $response = @stream_get_contents($fp);
            @fclose($fp);

            if ($response == '') $response = sprintf("The server returned no useable data. This likely points to a NULL result. Try installing php-curl for better error handling.\n");
        }
        
        else
        {
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_POST, TRUE);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
            $response = curl_exec($curl);

            if ($response == '') $response = sprintf("cURL Error %s: %s\n", curl_errno($curl), curl_error($curl));

            curl_close($curl);
        }

        if ($this->response_format == 'php') $response = json_decode($response);

        return $response;
    }

    private function __getUrl()
    {
        return sprintf('https://%s/%s/site/%s', $this->host, $this->short_name, $this->__servlet);
    }

    public function call($servletMethod, $params = NULL)
    {
        $this->__servlet = array_shift(explode('_', $servletMethod));
        $this->__method  = array_pop(explode('_', $servletMethod));
        if ($params !== NULL) $this->__methodParams = $params;
        return $this->__makeCall();
    }

}

I have auto-loaded this file as well called

$autoload['libraries'] = array( 'database', 'Session', 'Convioapi');

this is my login class where i plan to do all my login functionality

<?php

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

class Login extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        
        
        $this->load->helper(array('form'));
            //Load form helper
        $this->load->helper('form');
        
        //Load form_validation
        $this->load->library('form_validation');
        
        //Load session Library
        $this->load->library('Session');
    
    }

 
    public function index()
    {
        $this->load->view('login');
    }
    
    public function userLogin(){
    //form validation set rules

    $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');

    if ($this->form_validation->run() == false){
    if(isset($this->session->userdata['logged_in'])){
        $this->load->view('memberArea'); //member search area
        }else{
        $this->load->view('login'); //login 
        }
    } else {

        $userdata = array(
            'username' => $this->input->post('username'),
            'password' => $this->input->post('password'),
            'servletMethod'=> 'SRConsAPI_login'
            );
        
//              $this->load->library('ConvioOpenAPI', $data);       
        /*
        check if user exist in convio database using API 
        if true add session usrname to session data array
        and set userdata logged in $session data
        lastly allow user to enter member search view
        */      
        $groupdata = array(
            'username' => $this->input->post('username'),
            'password' => $this->input->post('password'),
            'servletMethod'=> 'SRConsAPI_getUserGroups'
            );
        
        $jsonUserString = $this->load->library('Convioapi', $userdata);
        $jsongroupString = $this->load->library('Convioapi', $groupdata);
        
        $arrayUser = json_decode($jsonUserString, true);
        $arrayGroup = json_decode($jsongroupString, true);
        
        if (!array_key_exists('errorResponse', $arrayUser) && 
            !array_key_exists('errorResponse', $arrayGroup))
        {
            
            if ( array_key_exists('cons_id', $arrayUser['loginResponse']) && array_key_exists('group', $arrayGroup['getConsGroupsResponse'])) {
                
                if ($arrayUser['loginResponse']['cons_id'] && $arrayUser['getConsGroupsResponse']['group']['id'] == '104'){
                    echo 'good';
                    //user goood
                }else{
                    echo 'bad';
                    //user bad
                }
                
            }else{
                
                return false;
}
            
        }else{
            return false;
        }
            
        }
    }

    public function logout(){

        //removing session data
        $sess_array = array(

            'username' => ''
        );
        $this->session->unset_userdata('logged in', $sess_array);
        $data['message_display'] = 'Successfullu Logged out';
        $this->load->view('login_form', $data);
    }
    

}

/* End of file login.php / / Location: ./application/controllers/login.php/

Community
  • 1
  • 1
luziio
  • 13
  • 3

1 Answers1

0

1 - Add library file under system/libraries folder.
2 - Make sure that first letters are capitalized(file name and class name).
3 - Then edit autoload.php's related line like this:
$autoload['libraries'] = array( 'database', 'session', 'convioapi');

It has to work perfectly!

Deniz B.
  • 2,532
  • 1
  • 18
  • 35
  • It did, so weird. Thanks so much tho! I wish I could mark this as the answer I dont have enough reputation – luziio Dec 16 '15 at 21:12
  • Don't try to click up vote button, just click on checkmark under vote buttons. Happy coding! – Deniz B. Dec 17 '15 at 07:05
  • I just have the doubt that happens when updating the version of codeigniter and this folder is replaced, well I suppose you should make a copy? But the correct path is not application / libraries / MY_library.php – JG_GJ Jun 03 '21 at 21:23