179 $session = curl_init($url);
181 $curl_options = array();
182 foreach ($defaultParams as $defkey => $defval) {
183 if (isset($curl_params[$defkey])) {
184 $curl_options[$defkey] = $curl_params[$defkey];
186 $curl_options[$defkey] = $defaultParams[$defkey];
189 foreach ($curl_params as $defkey => $defval) {
190 if (!isset($curl_options[$defkey])) {
191 $curl_options[$defkey] = $curl_params[$defkey];
195 dol_syslog(
"curl curl_options = ".var_export($curl_options,
true));
196 curl_setopt_array($session, $curl_options);
197 $response = curl_exec($session);
199 $index = strpos($response,
"\r\n\r\n");
200 if ($index ===
false && $curl_params[CURLOPT_CUSTOMREQUEST] !=
'HEAD') {
204 $header = substr($response, 0, $index);
205 $body = substr($response, $index + 4);
207 $headerArrayTmp = explode(
"\n", $header);
209 $headerArray = array();
210 foreach ($headerArrayTmp as &$headerItem) {
211 $tmp = explode(
':', $headerItem);
212 $tmp = array_map(
'trim', $tmp);
213 if (count($tmp) == 2) {
214 $headerArray[$tmp[0]] = $tmp[1];
218 if (array_key_exists(
'PSWS-Version', $headerArray)) {
219 $this->version = $headerArray[
'PSWS-Version'];
221 version_compare(PrestaShopWebservice::PS_COMPATIBLE_VERSIONS_MIN, $headerArray[
'PSWS-Version']) == 1 ||
222 version_compare(PrestaShopWebservice::PS_COMPATIBLE_VERSIONS_MAX, $headerArray[
'PSWS-Version']) == -1
225 'This library is not compatible with this version of PrestaShop. Please upgrade/downgrade this library'
231 $this->
printDebug(
'HTTP REQUEST HEADER', curl_getinfo($session, CURLINFO_HEADER_OUT));
232 $this->
printDebug(
'HTTP RESPONSE HEADER', $header);
234 $status_code = curl_getinfo($session, CURLINFO_HTTP_CODE);
235 if ($status_code === 0) {
238 curl_close($session);
240 if ($curl_params[CURLOPT_CUSTOMREQUEST] ==
'PUT' || $curl_params[CURLOPT_CUSTOMREQUEST] ==
'POST') {
241 $this->
printDebug(
'XML SENT', urldecode($curl_params[CURLOPT_POSTFIELDS]));
243 if ($curl_params[CURLOPT_CUSTOMREQUEST] !=
'DELETE' && $curl_params[CURLOPT_CUSTOMREQUEST] !=
'HEAD') {
247 return array(
'status_code' => $status_code,
'response' => $body,
'header' => $header);
290 if ($response !=
'') {
291 libxml_clear_errors();
292 libxml_use_internal_errors(
true);
293 if (LIBXML_VERSION < 20900) {
297 libxml_disable_entity_loader(
true);
300 $xml = simplexml_load_string(trim($response),
'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NONET);
301 if (libxml_get_errors()) {
302 $msg = var_export(libxml_get_errors(),
true);
303 libxml_clear_errors();
377 public function get($options)
379 if (isset($options[
'url'])) {
380 $url = $options[
'url'];
381 } elseif (isset($options[
'resource'])) {
382 $url = $this->url .
'/api/' . $options[
'resource'];
383 $url_params = array();
384 if (isset($options[
'id'])) {
385 $url .=
'/' . $options[
'id'];
388 $params = array(
'filter',
'display',
'sort',
'limit',
'id_shop',
'id_group_shop',
'schema',
'language',
'date',
'price');
389 foreach ($params as $p) {
390 foreach ($options as $k => $o) {
391 if (strpos($k, $p) !==
false) {
392 $url_params[$k] = $options[$k];
396 if (count($url_params) > 0) {
397 $url .=
'?' . http_build_query($url_params);
403 $request = $this->
executeRequest($url, array(CURLOPT_CUSTOMREQUEST =>
'GET'));
407 return $this->
parseXML($request[
'response']);
420 if (isset($options[
'url'])) {
421 $url = $options[
'url'];
422 } elseif (isset($options[
'resource'])) {
423 $url = $this->url .
'/api/' . $options[
'resource'];
424 $url_params = array();
425 if (isset($options[
'id'])) {
426 $url .=
'/' . $options[
'id'];
429 $params = array(
'filter',
'display',
'sort',
'limit');
430 foreach ($params as $p) {
431 foreach ($options as $k => $o) {
432 if (strpos($k, $p) !==
false) {
433 $url_params[$k] = $options[$k];
437 if (count($url_params) > 0) {
438 $url .=
'?' . http_build_query($url_params);
443 $request = $this->
executeRequest($url, array(CURLOPT_CUSTOMREQUEST =>
'HEAD', CURLOPT_NOBODY =>
true));
445 return $request[
'header'];