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