first commit
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use GuzzleHttp\Exception\ServerException;
|
||||
|
||||
class SQEAPI
|
||||
{
|
||||
private static function handleSQEAPIResponse($response)
|
||||
{
|
||||
$res = json_decode($response->getBody());
|
||||
|
||||
$data = null;
|
||||
$error = null;
|
||||
$success = false;
|
||||
|
||||
if (!isset($res)) {
|
||||
return show_error(500);
|
||||
}
|
||||
|
||||
if (property_exists($res, 'success')) {
|
||||
$success = $res->success;
|
||||
}
|
||||
|
||||
if (property_exists($res, 'message')) {
|
||||
$error = $res->message;
|
||||
}
|
||||
|
||||
if (property_exists($res, 'data')) {
|
||||
$data = $res->data;
|
||||
}
|
||||
|
||||
return array_merge((array) $res, ['data' => $data, 'success' => $success, 'error' => $error]);
|
||||
}
|
||||
|
||||
public static function get($url, $params)
|
||||
{
|
||||
global $savvyext;
|
||||
$token = $savvyext->cfgReadChar('system.oauth2_token');
|
||||
$baseUri = $savvyext->cfgReadChar('system.sqe_api_url');
|
||||
|
||||
$client = new Client(['base_uri' => $baseUri]);
|
||||
$header = ['Authorization' => $token];
|
||||
|
||||
try {
|
||||
$response = $client->request('GET', $url, [
|
||||
'headers' => [
|
||||
'Authorization' => $token,
|
||||
],
|
||||
'query' => $params,
|
||||
]);
|
||||
} catch (ServerException $e) {
|
||||
$response = $e->getResponse();
|
||||
} catch (ClientException $e) {
|
||||
$response = $e->getResponse();
|
||||
}
|
||||
|
||||
return self::handleSQEAPIResponse($response);
|
||||
}
|
||||
|
||||
public static function post($url, $params)
|
||||
{
|
||||
global $savvyext;
|
||||
$token = $savvyext->cfgReadChar('system.oauth2_token');
|
||||
$baseUri = $savvyext->cfgReadChar('system.api_url');
|
||||
|
||||
$client = new Client(['base_uri' => $baseUri]);
|
||||
$header = ['Authorization' => $token, 'Content-Type' => 'application/json'];
|
||||
try {
|
||||
$response = $client->request('POST', $url, [
|
||||
'headers' => $header,
|
||||
'body' => json_encode($params),
|
||||
]);
|
||||
} catch (ServerException $e) {
|
||||
$response = $e->getResponse();
|
||||
} catch (ClientException $e) {
|
||||
$response = $e->getResponse();
|
||||
}
|
||||
|
||||
return self::handleSQEAPIResponse($response);
|
||||
}
|
||||
|
||||
public static function put($url, $params)
|
||||
{
|
||||
global $savvyext;
|
||||
$token = $savvyext->cfgReadChar('system.oauth2_token');
|
||||
$baseUri = $savvyext->cfgReadChar('system.api_url');
|
||||
|
||||
$client = new Client(['base_uri' => $baseUri]);
|
||||
$headers = ['Authorization' => $token, 'Content-Type' => 'text/plain'];
|
||||
|
||||
try {
|
||||
$response = $client->request('PUT', $url, [
|
||||
'headers' => $headers,
|
||||
'body' => $params,
|
||||
]);
|
||||
} catch (ServerException $e) {
|
||||
$response = $e->getResponse();
|
||||
} catch (ClientException $e) {
|
||||
$response = $e->getResponse();
|
||||
}
|
||||
|
||||
return self::handleSQEAPIResponse($response);
|
||||
}
|
||||
|
||||
public static function delete($url)
|
||||
{
|
||||
global $savvyext;
|
||||
$token = $savvyext->cfgReadChar('system.oauth2_token');
|
||||
$baseUri = $savvyext->cfgReadChar('system.api_url');
|
||||
|
||||
$client = new Client(['base_uri' => $baseUri]);
|
||||
$headers = ['Authorization' => $token];
|
||||
|
||||
try {
|
||||
$response = $client->request('DELETE', $url, [
|
||||
'headers' => $headers,
|
||||
]);
|
||||
} catch (ServerException $e) {
|
||||
$response = $e->getResponse();
|
||||
} catch (ClientException $e) {
|
||||
$response = $e->getResponse();
|
||||
}
|
||||
|
||||
return self::handleSQEAPIResponse($response);
|
||||
}
|
||||
|
||||
public static function geocode($addr) {
|
||||
|
||||
global $savvyext;
|
||||
$httpAuthToken = $savvyext->cfgReadChar('system.oauth2_token');
|
||||
$encryptionAlg = $savvyext->cfgReadChar('encryption.algorithm');
|
||||
$encryptionKey = $savvyext->cfgReadChar('encryption.key');
|
||||
$encryptionIV = $savvyext->cfgReadChar('encryption.iv');
|
||||
$api_url = $savvyext->cfgReadChar('system.api_url');
|
||||
|
||||
$url = $api_url . "trips/api/geocode/?address=" . urlencode($addr);
|
||||
|
||||
$ch = curl_init($url);
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_VERBOSE, false);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
|
||||
'Content-Type: application/json',
|
||||
'Authorization: Server-Token ' . $httpAuthToken,
|
||||
"client_id: BackOffice"
|
||||
)
|
||||
);
|
||||
|
||||
$body = curl_exec($ch);
|
||||
$result = json_decode($body, true);
|
||||
|
||||
$payload = openssl_decrypt(
|
||||
hex2bin(
|
||||
$result['payload']
|
||||
), $encryptionAlg, $encryptionKey, OPENSSL_RAW_DATA, $encryptionIV
|
||||
);
|
||||
return json_decode($payload, true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user