48 const PSCOMPATIBLEVERSIONMIN =
'1.4.0.0';
49 const PSCOMPATIBLEVERSIONMAX =
'1.7.99.99';
74 if (!extension_loaded(
'curl')) {
79 $this->debug = $debug;
80 $this->version =
'unknown';
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);
209 echo
'<div style="display:table;background:#CCC;font-size:8pt;padding:7px"><h6 style="font-size:9pt;margin:0">'.dol_escape_htmltag($title).
'</h6><pre>'.
dol_escape_htmltag($content).
'</pre></div>';
219 return $this->version;
232 if ($response !=
'') {
233 libxml_clear_errors();
234 libxml_use_internal_errors(
true);
235 if (!function_exists(
'simplexml_load_string')) {
238 $xml = simplexml_load_string($response,
'SimpleXMLElement', LIBXML_NOCDATA|LIBXML_NONET);
239 if (libxml_get_errors()) {
240 $msg = var_export(libxml_get_errors(),
true);
241 libxml_clear_errors();
262 public function add($options)
267 if (isset($options[
'resource'], $options[
'postXml']) || isset($options[
'url'], $options[
'postXml'])) {
268 $url = (isset($options[
'resource']) ? $this->url.
'/api/'.$options[
'resource'] : $options[
'url']);
269 $xml = $options[
'postXml'];
270 if (isset($options[
'id_shop'])) {
271 $url .=
'&id_shop='.$options[
'id_shop'];
273 if (isset($options[
'id_group_shop'])) {
274 $url .=
'&id_group_shop='.$options[
'id_group_shop'];
279 $request = $this->
executeRequest($url, array(CURLOPT_CUSTOMREQUEST =>
'POST', CURLOPT_POSTFIELDS => $xml));
282 return $this->
parseXML($request[
'response']);
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'];
401 if (isset($options[
'url'])) {
402 $url = $options[
'url'];
403 } elseif ((isset($options[
'resource'], $options[
'id']) || isset($options[
'url'])) && $options[
'putXml']) {
404 $url = (isset($options[
'url']) ? $options[
'url'] : $this->url.
'/api/'.$options[
'resource'].
'/'.$options[
'id']);
405 $xml = $options[
'putXml'];
406 if (isset($options[
'id_shop'])) {
407 $url .=
'&id_shop='.$options[
'id_shop'];
409 if (isset($options[
'id_group_shop'])) {
410 $url .=
'&id_group_shop='.$options[
'id_group_shop'];
416 $request = $this->
executeRequest($url, array(CURLOPT_CUSTOMREQUEST =>
'PUT', CURLOPT_POSTFIELDS => $xml));
418 return $this->
parseXML($request[
'response']);
add($options)
Add (POST) a resource.
printDebug($title, $content)
Output debug info.
edit($options)
Edit (PUT) a resource.
parseXML($response)
Load XML from string.
head($options)
Head method (HEAD) a resource.
__construct($url, $key, $debug=true)
PrestaShopWebservice constructor.
checkStatusCode($status_code)
Take the status code and throw an exception if the server didn't return 200 or 201 code.
executeRequest($url, $curl_params=array())
Handles a CURL request to PrestaShop Webservice.
getVersion()
Return version.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.