Consume a REST API in codeigniter controller
03:31 13 Aug 2018

I have create a REST API and want to consume my own created API in codeigniter controller.

My created REST API controller(example.php)

   class Example extends REST_Controller {

    public function __construct() { 
    parent::__construct();        
    $this->load->model('user');
   }

     public function user_fetch_post() {
    //returns all rows if the id parameter doesn't exist,
    //otherwise single row will be returned
    $id = $this->input->post('id');
    $users = $this->user->getRows($id);

    //check if the user data exists
    if(!empty($users)){
        //set the response and exit
        $this->response($users, REST_Controller::HTTP_OK);
    }else{
        //set the response and exit
        $this->response([
            'status' => FALSE,
            'message' => 'No user were found.'
        ], REST_Controller::HTTP_NOT_FOUND);
    }
    }

model(user.php)

    function getRows($id = ""){
    if(!empty($id)){
        $query = $this->db->get_where('users', array('id' => $id));
        return $query->row_array();
    }else{
        $query = $this->db->get('users');
        return $query->result_array();
    }
    }

Here i want to call my created api(from example.php)for fetch record in welcome.php controller with basic authentication(uname-admin,pwd-1234)

my controller welcome.php

public function index()
{
}

Can anybody help to me that how to call my api in controller welcome.php with basic authentication.

rest codeigniter codeigniter-query-builder codeigniter-restapi