206 lines
5.9 KiB
PHP
206 lines
5.9 KiB
PHP
<?php
|
|
/**
|
|
* Module made by Nukium
|
|
*
|
|
* @author Nukium
|
|
* @copyright 2018 Nukium SAS
|
|
* @license All rights reserved
|
|
*
|
|
* ███ ██ ██ ██ ██ ██ ██ ██ ██ ███ ███
|
|
* ████ ██ ██ ██ ██ ██ ██ ██ ██ ████ ████
|
|
* ██ ██ ██ ██ ██ █████ ██ ██ ██ ██ ████ ██
|
|
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
|
|
* ██ ████ ██████ ██ ██ ██ ██████ ██ ██
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
* THE SOFTWARE.
|
|
*/
|
|
|
|
class GlsApi
|
|
{
|
|
/**
|
|
* REST API URL (test/prod)
|
|
* @var string
|
|
*/
|
|
private $rest_api_url_test = 'https://api-qs.gls-group.eu/public/v1/';
|
|
private $rest_api_url = 'https://api.gls-group.eu/public/v1/';
|
|
|
|
/**
|
|
* Login de la Web API GLS
|
|
* @var string $api_login
|
|
*/
|
|
private $api_login = '';
|
|
|
|
/**
|
|
* Mot de passe de la Web API GLS
|
|
* @var string $api_pwd
|
|
*/
|
|
private $api_pwd = '';
|
|
|
|
/**
|
|
* Debug
|
|
* @var bool $debug
|
|
*/
|
|
private $debug = false;
|
|
|
|
/**
|
|
* Test mode ON/OFF (defaut: OFF)
|
|
* @var bool $test_mode
|
|
*/
|
|
private $test_mode = false;
|
|
|
|
/**
|
|
* Debug
|
|
* @var bool $debug
|
|
*/
|
|
private $lang = 'en';
|
|
|
|
/**
|
|
* Return error
|
|
* @var array
|
|
*/
|
|
public $error = array();
|
|
|
|
public function __construct($login, $pwd, $_lang, $test_mode = false, $debug = false)
|
|
{
|
|
if (!extension_loaded('curl')) {
|
|
return false;
|
|
}
|
|
|
|
if (!empty($login) && !empty($pwd)) {
|
|
$this->api_login = $login;
|
|
$this->api_pwd = $pwd;
|
|
} else {
|
|
return false;
|
|
}
|
|
|
|
if (Tools::strlen($_lang) == 2) {
|
|
$this->lang = $_lang;
|
|
} else {
|
|
// Bad iso language ISO 3166-2 2
|
|
return false;
|
|
}
|
|
|
|
$this->debug = $debug;
|
|
|
|
$this->test_mode = $test_mode;
|
|
}
|
|
|
|
private function executeRequest($_method, $_resource, $_body = '')
|
|
{
|
|
$credentials = $this->api_login.':'.$this->api_pwd;
|
|
|
|
if ($_body) {
|
|
$data = json_encode($_body);
|
|
} else {
|
|
$data = '';
|
|
}
|
|
|
|
// Call
|
|
if ($this->test_mode) {
|
|
$url = $this->rest_api_url_test;
|
|
} else {
|
|
$url = $this->rest_api_url;
|
|
}
|
|
$session = curl_init($url.$_resource);
|
|
|
|
$curl_options = array(
|
|
CURLOPT_RETURNTRANSFER => 1,
|
|
CURLOPT_TIMEOUT => 60,
|
|
CURLOPT_ENCODING => 'gzip,deflate',
|
|
CURLOPT_HTTPHEADER => array(
|
|
'Accept-Language: ' . $this->lang,
|
|
'Accept: application/json',
|
|
'Content-Type: application/json',
|
|
'Authorization: Basic ' . base64_encode($credentials)
|
|
),
|
|
CURLOPT_CUSTOMREQUEST => $_method,
|
|
);
|
|
|
|
if (!empty($_body)) {
|
|
$curl_options[CURLOPT_POSTFIELDS] = $data;
|
|
}
|
|
|
|
curl_setopt_array($session, $curl_options);
|
|
$response = curl_exec($session);
|
|
|
|
if ($response === false) {
|
|
$this->error[] = 'Bad response';
|
|
}
|
|
|
|
if (empty($this->error)) {
|
|
if ($this->debug) {
|
|
$this->printDebug('HTTP REQUEST HEADER', curl_getinfo($session, CURLINFO_HEADER_OUT));
|
|
if (!empty($_body)) {
|
|
$this->printDebug('DATA SENT', $curl_options[CURLOPT_POSTFIELDS]);
|
|
}
|
|
}
|
|
// $status_code = curl_getinfo($session, CURLINFO_HTTP_CODE);
|
|
|
|
$result = json_decode($response);
|
|
if (isset($result->errors) && is_array($result->errors) && count($result->errors) > 0) {
|
|
foreach ($result->errors as $value) {
|
|
$this->error[] = array('code' => $value->exitCode, 'message' => $value->description);
|
|
}
|
|
}
|
|
|
|
// if ($status_code === 201) {
|
|
// } elseif ($status_code === 400) {
|
|
// $this->error[] = 'Bad Request';
|
|
// } elseif ($status_code === 401) {
|
|
// $this->error[] = 'Unauthorized';
|
|
// } elseif ($status_code === 403) {
|
|
// $this->error[] = 'Forbidden';
|
|
// } elseif ($status_code === 0) {
|
|
// $this->error[] = 'CURL Error: '.curl_error($session);
|
|
// } else {
|
|
// $this->error[] = 'Other error';
|
|
// }
|
|
|
|
curl_close($session);
|
|
}
|
|
|
|
if (count($this->error) > 0) {
|
|
if ($this->debug) {
|
|
foreach ($this->error as $value) {
|
|
$this->printDebug('Error', $value);
|
|
}
|
|
}
|
|
return false;
|
|
} else {
|
|
return $result;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* POST request
|
|
* @param string $_url Resource
|
|
* @param array $_body or empty if not
|
|
*/
|
|
public function post($_resource, $_body = '')
|
|
{
|
|
return $this->executeRequest('POST', $_resource, $_body);
|
|
}
|
|
|
|
/**
|
|
* Print debug
|
|
* @param unknown $_title
|
|
* @param unknown $_content
|
|
*/
|
|
public function printDebug($_title, $_content)
|
|
{
|
|
if (is_array($_content)) {
|
|
foreach ($_content as $key => $value) {
|
|
echo $key.': '.$value;
|
|
}
|
|
} else {
|
|
echo $_title.': '.$_content;
|
|
}
|
|
}
|
|
}
|