API integration in Codeigniter application API integration in Codeigniter application codeigniter codeigniter

API integration in Codeigniter application


I'm not a specialist of CodeIgniter, but after having a look to the documentation, and some links (like this and this) you can create your own library like this :

<?php                                                                                   defined('BASEPATH') OR exit('No direct script access allowed');/** * @description : Library to access MyOperator Public API */Class Myoperator {    protected $developers_url = 'https://developers.myoperator.co/';    protected $token = 'XXXXXXXXX';    function __construct() {    }    public function run() {        # request for Logs        $url = $this->developers_url . 'search';        $fields = array("token" => $this->token);        $result = $this->_post_api($fields, $url);        $this->log("result");        $this->log($result);    }    private function _post_api(Array $fields, $url) {        try {            $ch = curl_init();            curl_setopt($ch, CURLOPT_URL, $url);            curl_setopt($ch, CURLOPT_TIMEOUT, 30);            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);            $result = curl_exec($ch);        } catch (Exception $e) {            return false;        }        $this->log("url");        $this->log($url);        $this->log("fields");        $this->log($fields);        curl_close($ch);        if ($result)            return $result;        else            return false;    }    private function log($message) {        print_r($message);        echo "\n";    }}?>

After that, you can get the library from the controller, like that :

<?phpclass Operator extends CI_Controller {   public function index()   {       $this->load->library('operator');         $this->operator->run();   }}?>

Edit (Error handling)

<?phpclass Operator extends CI_Controller {   public function index()   {       try {           $this->load->library('operator');             $this->operator->run();       } catch (Exception $e) {           var_dump($e->getMessage());       }   }}?>

Hope this helps !


I run this code directly, but I'm confuse that where to use this code, which part is in controller and which part is in view in CodeIgniter.

It sounds like a quick primer on the structure of a CodeIgniter application is in order. A default CI 3.1.3 release has this structure:

.├── application/├── composer.json├── contributing.md├── index.php├── license.txt├── readme.rst├── system/└── user_guide/

Your application, including your supplied library, will reside inside the application directory. In your case, the "Myoperator" library should be placed in application/libraries. Your controller will reside in application/controllers and views in application/views.

In your final Myoperator.php file, you'll need to remove the last two lines that you have in your example. Those will be replaced by their equivalents in your controller.

In your controller, you simply need to load the library like this:

$this->load->library('myoperator');

Then invoke the library like this:

$this->myoperator->run();

This is the CodeIgniter equivalent of those last 2 lines, $Class = new Myoperator(); and $Class->run();


In case they have given any library then you use that by including that library in your controller file then directly call their function.If they have provided you a code then you have to create your own api file to call their url .Just code paste the code into the web/application/libraries/Operator.php(assuming the name of library).

Operator.php

<?php                                                                                   /** * @description : Library to access MyOperator Public API */Class Myoperator {    protected $developers_url = 'https://developers.myoperator.co/';    protected $token = 'XXXXXXXXX';    function __construct() {    }    public function run() {        # request for Logs        $url = $this->developers_url . 'search';        $fields = array("token" => $this->token);        $result = $this->_post_api($fields, $url);        $this->log("result");        $this->log($result);    }    private function _post_api(Array $fields, $url) {        try {            $ch = curl_init();            curl_setopt($ch, CURLOPT_URL, $url);            curl_setopt($ch, CURLOPT_TIMEOUT, 30);            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);            $result = curl_exec($ch);        } catch (Exception $e) {            return false;        }        $this->log("url");        $this->log($url);        $this->log("fields");        $this->log($fields);        curl_close($ch);        if ($result)            return $result;        else            return false;    }    private function log($message) {        print_r($message);        echo "\n";    }}

Then in your custom controller include the api and extend the controller with it.And then call the function of that api automatically.

require('application/libraries/Operator.php');class Users extends Operator{    public function __Construct()    {       $this->run(); /*You can call the function directly using $this*/    }}

I don't know the working of the api but the method you can try this.I hope it will work.