91 $error_label =
'This call to PrestaShop Web Services failed and returned an HTTP status of %d. That means: %s.';
92 switch ($status_code) {
122 $defaultParams = array(
123 CURLOPT_HEADER =>
true,
124 CURLOPT_RETURNTRANSFER =>
true,
125 CURLINFO_HEADER_OUT =>
true,
126 CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
127 CURLOPT_USERPWD => $this->key.
':',
128 CURLOPT_HTTPHEADER => array(
'Expect:')
132 $session = curl_init($url);
134 $curl_options = array();
135 foreach ($defaultParams as $defkey => $defval) {
136 if (isset($curl_params[$defkey])) {
137 $curl_options[$defkey] = $curl_params[$defkey];
139 $curl_options[$defkey] = $defaultParams[$defkey];
142 foreach ($curl_params as $defkey => $defval) {
143 if (!isset($curl_options[$defkey])) {
144 $curl_options[$defkey] = $curl_params[$defkey];
148 dol_syslog(
"curl curl_options = ".var_export($curl_options,
true));
149 curl_setopt_array($session, $curl_options);
150 $response = curl_exec($session);
152 $index = strpos($response,
"\r\n\r\n");
153 if ($index ===
false && $curl_params[CURLOPT_CUSTOMREQUEST] !=
'HEAD') {
157 $header = substr($response, 0, $index);
158 $body = substr($response, $index + 4);
160 $headerArrayTmp = explode(
"\n", $header);
162 $headerArray = array();
163 foreach ($headerArrayTmp as &$headerItem) {
164 $tmp = explode(
':', $headerItem);
165 $tmp = array_map(
'trim', $tmp);
166 if (count($tmp) == 2) {
167 $headerArray[$tmp[0]] = $tmp[1];
171 if (array_key_exists(
'PSWS-Version', $headerArray)) {
172 $this->version = $headerArray[
'PSWS-Version'];
173 if (version_compare(PrestaShopWebservice::PSCOMPATIBLEVERSIONMIN, $headerArray[
'PSWS-Version']) == 1 ||
174 version_compare(PrestaShopWebservice::PSCOMPATIBLEVERSIONMAX, $headerArray[
'PSWS-Version']) == -1
176 throw new PrestaShopWebserviceException(
'This library is not compatible with this version of PrestaShop. Please upgrade/downgrade this library');
181 $this->
printDebug(
'HTTP REQUEST HEADER', curl_getinfo($session, CURLINFO_HEADER_OUT));
182 $this->
printDebug(
'HTTP RESPONSE HEADER', $header);
184 $status_code = curl_getinfo($session, CURLINFO_HTTP_CODE);
185 if ($status_code === 0) {
188 curl_close($session);
190 if ($curl_params[CURLOPT_CUSTOMREQUEST] ==
'PUT' || $curl_params[CURLOPT_CUSTOMREQUEST] ==
'POST') {
191 $this->
printDebug(
'XML SENT', urldecode($curl_params[CURLOPT_POSTFIELDS]));
193 if ($curl_params[CURLOPT_CUSTOMREQUEST] !=
'DELETE' && $curl_params[CURLOPT_CUSTOMREQUEST] !=
'HEAD') {
197 return array(
'status_code' => $status_code,
'response' => $body,
'header' => $header);
315 public function get($options)
317 if (isset($options[
'url'])) {
318 $url = $options[
'url'];
319 } elseif (isset($options[
'resource'])) {
320 $url = $this->url.
'/api/'.$options[
'resource'];
321 $url_params = array();
322 if (isset($options[
'id'])) {
323 $url .=
'/'.$options[
'id'];
328 $params = array(
'filter',
'display',
'sort',
'limit',
'id_shop',
'id_group_shop',
'date');
329 foreach ($params as $p) {
330 foreach ($options as $k => $o) {
331 if (strpos($k, $p) !==
false) {
332 $url_params[$k] = $options[$k];
336 if (count($url_params) > 0) {
337 $url .=
'?'.http_build_query($url_params);
343 $request = $this->
executeRequest($url, array(CURLOPT_CUSTOMREQUEST =>
'GET'));
345 return $this->
parseXML($request[
'response']);
358 if (isset($options[
'url'])) {
359 $url = $options[
'url'];
360 } elseif (isset($options[
'resource'])) {
361 $url = $this->url.
'/api/'.$options[
'resource'];
362 $url_params = array();
363 if (isset($options[
'id'])) {
364 $url .=
'/'.$options[
'id'];
367 $params = array(
'filter',
'display',
'sort',
'limit');
368 foreach ($params as $p) {
369 foreach ($options as $k => $o) {
370 if (strpos($k, $p) !==
false) {
371 $url_params[$k] = $options[$k];
375 if (count($url_params) > 0) {
376 $url .=
'?'.http_build_query($url_params);
381 $request = $this->
executeRequest($url, array(CURLOPT_CUSTOMREQUEST =>
'HEAD', CURLOPT_NOBODY =>
true));
383 return $request[
'header'];