File: /home/cafsindia/ntc_cafsinfotech_in/application/controllers/Gpstracking_api.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Base_controller.php");
class Gpstracking_api extends Base_controller{
// COMMON PREFIX URL
public function url(){
return $url="http://vplus.vigil.co.za/ntc/api/";
}
// GETTING TOKEN VALUE USING USERNAME AND PASSWORD
public function token(){
$result = $this->curl('login/NTCAPI1/password');
$token = json_decode($result);
return $token->Token;
}
// REPLACING EMPTY ARRAY AS NO DATA
public function replace_empty_values($result){
foreach($result as &$val){
if(is_array($val))
$val = $this->replace_empty_values($val);
else
if($val === "" || $val === false || $val === null)
$val = "data not found";
}
return $result;
}
// GETTING VEHICLE INFORMATION
public function vehicle(){
$url_data = $this->token().'/vehicles';
$result = $this->curl($url_data);
$result = json_decode($result,true);
echo json_encode($this->replace_empty_values($result));
}
// GETTING VEHICLE POSITION
public function vehicle_position(){
$prime_vehicle_master_id = (int)$this->input->post('prime_vehicle_master_id');
$vehicle_info_qry = 'SELECT asset_id,driver_id from cw_vehicle_master where prime_vehicle_master_id = "'.$prime_vehicle_master_id.'" and trans_status = 1';
$vehicle_info = $this->db->query("CALL sp_a_run ('SELECT','$vehicle_info_qry')");
$vehicle_result = $vehicle_info->row();
$vehicle_count = $vehicle_info->num_rows();
$vehicle_info->next_result();
if((int)$vehicle_count > 0){
$asset_id = $vehicle_result->asset_id;
$driver_id = $vehicle_result->driver_id;
$url_data = $this->token().'/vehicleLastPosition/'.$asset_id.'';
$result = $this->curl($url_data);
$result = json_decode($result);
if(!($result->Message) && $asset_id){
echo json_encode(array('success' => TRUE,'data' => $result));
}else{
echo json_encode(array('success' => FALSE,'data' => $result));
}
}
}
// CURL COMMON FUNCTION
public function curl($url_data){
$url=$this->url().$url_data;
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Getting value as JSON
curl_setopt($ch, CURLOPT_HTTPHEADER, 'application/json');
// Execute
$result = curl_exec($ch);
// Closing
curl_close($ch);
//return result
return $result;
}
}
?>