178 $session = curl_init($url);
180 $curl_options = array();
181 foreach ($defaultParams as $defkey => $defval) {
182 if (isset($curl_params[$defkey])) {
183 $curl_options[$defkey] = $curl_params[$defkey];
185 $curl_options[$defkey] = $defaultParams[$defkey];
188 foreach ($curl_params as $defkey => $defval) {
189 if (!isset($curl_options[$defkey])) {
190 $curl_options[$defkey] = $curl_params[$defkey];
194 dol_syslog(
"curl curl_options = ".var_export($curl_options,
true));
195 curl_setopt_array($session, $curl_options);
196 $response = curl_exec($session);
198 $index = strpos($response,
"\r\n\r\n");
199 if ($index ===
false && $curl_params[CURLOPT_CUSTOMREQUEST] !=
'HEAD') {
203 $header = substr($response, 0, $index);
204 $body = substr($response, $index + 4);
206 $headerArrayTmp = explode(
"\n", $header);
208 $headerArray = array();
209 foreach ($headerArrayTmp as &$headerItem) {
210 $tmp = explode(
':', $headerItem);
211 $tmp = array_map(
'trim', $tmp);
212 if (count($tmp) == 2) {
213 $headerArray[$tmp[0]] = $tmp[1];
217 if (array_key_exists(
'PSWS-Version', $headerArray)) {
218 $this->version = $headerArray[
'PSWS-Version'];
220 version_compare(PrestaShopWebservice::PS_COMPATIBLE_VERSIONS_MIN, $headerArray[
'PSWS-Version']) == 1 ||
221 version_compare(PrestaShopWebservice::PS_COMPATIBLE_VERSIONS_MAX, $headerArray[
'PSWS-Version']) == -1
224 'This library is not compatible with this version of PrestaShop. Please upgrade/downgrade this library'
230 $this->
printDebug(
'HTTP REQUEST HEADER', curl_getinfo($session, CURLINFO_HEADER_OUT));
231 $this->
printDebug(
'HTTP RESPONSE HEADER', $header);
233 $status_code = curl_getinfo($session, CURLINFO_HTTP_CODE);
234 if ($status_code === 0) {
237 curl_close($session);
239 if ($curl_params[CURLOPT_CUSTOMREQUEST] ==
'PUT' || $curl_params[CURLOPT_CUSTOMREQUEST] ==
'POST') {
240 $this->
printDebug(
'XML SENT', urldecode($curl_params[CURLOPT_POSTFIELDS]));
242 if ($curl_params[CURLOPT_CUSTOMREQUEST] !=
'DELETE' && $curl_params[CURLOPT_CUSTOMREQUEST] !=
'HEAD') {
246 return array(
'status_code' => $status_code,
'response' => $body,
'header' => $header);
289 if ($response !=
'') {
290 libxml_clear_errors();
291 libxml_use_internal_errors(
true);
292 if (LIBXML_VERSION < 20900) {
296 libxml_disable_entity_loader(
true);
299 $xml = simplexml_load_string(trim($response),
'SimpleXMLElement', LIBXML_NOCDATA|LIBXML_NONET);
300 if (libxml_get_errors()) {
301 $msg = var_export(libxml_get_errors(),
true);
302 libxml_clear_errors();
376 public function get($options)
378 if (isset($options[
'url'])) {
379 $url = $options[
'url'];
380 } elseif (isset($options[
'resource'])) {
381 $url = $this->url .
'/api/' . $options[
'resource'];
382 $url_params = array();
383 if (isset($options[
'id'])) {
384 $url .=
'/' . $options[
'id'];
387 $params = array(
'filter',
'display',
'sort',
'limit',
'id_shop',
'id_group_shop',
'schema',
'language',
'date',
'price');
388 foreach ($params as $p) {
389 foreach ($options as $k => $o) {
390 if (strpos($k, $p) !==
false) {
391 $url_params[$k] = $options[$k];
395 if (count($url_params) > 0) {
396 $url .=
'?' . http_build_query($url_params);
402 $request = $this->
executeRequest($url, array(CURLOPT_CUSTOMREQUEST =>
'GET'));
406 return $this->
parseXML($request[
'response']);
419 if (isset($options[
'url'])) {
420 $url = $options[
'url'];
421 } elseif (isset($options[
'resource'])) {
422 $url = $this->url .
'/api/' . $options[
'resource'];
423 $url_params = array();
424 if (isset($options[
'id'])) {
425 $url .=
'/' . $options[
'id'];
428 $params = array(
'filter',
'display',
'sort',
'limit');
429 foreach ($params as $p) {
430 foreach ($options as $k => $o) {
431 if (strpos($k, $p) !==
false) {
432 $url_params[$k] = $options[$k];
436 if (count($url_params) > 0) {
437 $url .=
'?' . http_build_query($url_params);
442 $request = $this->
executeRequest($url, array(CURLOPT_CUSTOMREQUEST =>
'HEAD', CURLOPT_NOBODY =>
true));
444 return $request[
'header'];