API integration in Codeigniter application

10,212

Solution 1

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 :

<?php

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

?>

Edit (Error handling)

<?php

class 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 !

Solution 2

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();

Solution 3

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.

Solution 4

Create your own library in the following direction Application/libraries/ And paste the following code:

 <?php defined('BASEPATH') OR exit('No direct script access allowed'); 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 use the library in the controller, like that :

  public function index() {
   $this->load->library('operator');  
   $this->operator->run();}

Error handling:

public function index(){
   try {
       $this->load->library('operator');  
       $this->operator->run();
   } catch (Exception $e) {
       var_dump($e->getMessage());
   }}
Share:
10,212
Ganesh Aher
Author by

Ganesh Aher

About me, I'm simple and love to code. I made too much mistakes while coding and learned from them. I love to hear music and play chess.

Updated on June 12, 2022

Comments

  • Ganesh Aher
    Ganesh Aher almost 2 years

    I'm trying to integrate API from MyOperator, in my CRM project, which is developed in CodeIgniter. For that they provide me some code, but when I tried to use that code in my application, it give me JSON data with

    404 page not found error

    Here is the code that they provided :

    <?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";
        }
    
    }
    
    
    $Class = new Myoperator();
    $Class->run();
    

    This code gives me desired output if 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. Any kind of help is welcome, thanks in advance.