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; } } }