Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions Vendor/PrestaShopWebservice.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ class PrestaShopWebservice
/** @var boolean is debug activated */
protected $debug;

/** @var string PS version */
protected $version;

/** @var array compatible versions of PrestaShop Webservice */
const psCompatibleVersionsMin = '1.4.0.17';
const psCompatibleVersionsMax = '1.5.4.1';
const psCompatibleVersionsMin = '1.4.0.0';
const psCompatibleVersionsMax = '1.6.0.14';

/**
* PrestaShopWebservice constructor. Throw an exception when CURL is not installed/activated
Expand Down Expand Up @@ -73,6 +76,7 @@ function __construct($url, $key, $debug = true) {
$this->url = $url;
$this->key = $key;
$this->debug = $debug;
$this->version = 'unknown';
}

/**
Expand Down Expand Up @@ -148,6 +152,7 @@ protected function executeRequest($url, $curl_params = array())

if (array_key_exists('PSWS-Version', $headerArray))
{
$this->version = $headerArray['PSWS-Version'];
if (
version_compare(PrestaShopWebservice::psCompatibleVersionsMin, $headerArray['PSWS-Version']) == 1 ||
version_compare(PrestaShopWebservice::psCompatibleVersionsMax, $headerArray['PSWS-Version']) == -1
Expand All @@ -168,16 +173,23 @@ protected function executeRequest($url, $curl_params = array())
if ($this->debug)
{
if ($curl_params[CURLOPT_CUSTOMREQUEST] == 'PUT' || $curl_params[CURLOPT_CUSTOMREQUEST] == 'POST')
$this->printDebug('XML SENT', $curl_params[CURLOPT_POSTFIELDS]);
$this->printDebug('XML SENT', urldecode($curl_params[CURLOPT_POSTFIELDS]));
if ($curl_params[CURLOPT_CUSTOMREQUEST] != 'DELETE' && $curl_params[CURLOPT_CUSTOMREQUEST] != 'HEAD')
$this->printDebug('RETURN HTTP BODY', $body);
}
return array('status_code' => $status_code, 'response' => $body, 'header' => $header);
}

public function printDebug($title, $content)
{
echo '<div style="display:table;background:#CCC;font-size:8pt;padding:7px"><h6 style="font-size:9pt;margin:0">'.$title.'</h6><pre>'.htmlentities($content).'</pre></div>';
}

public function getVersion()
{
return $this->version;
}

/**
* Load XML from string. Can throw exception
* @param string $response String from a CURL response
Expand All @@ -187,10 +199,15 @@ protected function parseXML($response)
{
if ($response != '')
{
libxml_clear_errors();
libxml_use_internal_errors(true);
$xml = simplexml_load_string($response);
$xml = simplexml_load_string($response,'SimpleXMLElement', LIBXML_NOCDATA);
if (libxml_get_errors())
throw new PrestaShopWebserviceException('HTTP XML response is not parsable : '.var_export(libxml_get_errors(), true));
{
$msg = var_export(libxml_get_errors(), true);
libxml_clear_errors();
throw new PrestaShopWebserviceException('HTTP XML response is not parsable: '.$msg);
}
return $xml;
}
else
Expand All @@ -214,10 +231,14 @@ public function add($options)
{
$url = (isset($options['resource']) ? $this->url.'/api/'.$options['resource'] : $options['url']);
$xml = $options['postXml'];
if (isset($options['id_shop']))
$url .= '&id_shop='.$options['id_shop'];
if (isset($options['id_group_shop']))
$url .= '&id_group_shop='.$options['id_group_shop'];
}
else
throw new PrestaShopWebserviceException('Bad parameters given');
$request = self::executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => 'xml='.$xml));
$request = self::executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => $xml));

self::checkStatusCode($request['status_code']);
return self::parseXML($request['response']);
Expand Down Expand Up @@ -262,7 +283,7 @@ public function get($options)
if (isset($options['id']))
$url .= '/'.$options['id'];

$params = array('filter', 'display', 'sort', 'limit');
$params = array('filter', 'display', 'sort', 'limit', 'id_shop', 'id_group_shop');
foreach ($params as $p)
foreach ($options as $k => $o)
if (strpos($k, $p) !== false)
Expand Down Expand Up @@ -328,6 +349,10 @@ public function edit($options)
{
$url = (isset($options['url']) ? $options['url'] : $this->url.'/api/'.$options['resource'].'/'.$options['id']);
$xml = $options['putXml'];
if (isset($options['id_shop']))
$url .= '&id_shop='.$options['id_shop'];
if (isset($options['id_group_shop']))
$url .= '&id_group_shop='.$options['id_group_shop'];
}
else
throw new PrestaShopWebserviceException('Bad parameters given');
Expand Down Expand Up @@ -369,6 +394,10 @@ public function delete($options)
$url = $this->url.'/api/'.$options['resource'].'/?id=['.implode(',', $options['id']).']';
else
$url = $this->url.'/api/'.$options['resource'].'/'.$options['id'];
if (isset($options['id_shop']))
$url .= '&id_shop='.$options['id_shop'];
if (isset($options['id_group_shop']))
$url .= '&id_group_shop='.$options['id_group_shop'];
$request = self::executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'DELETE'));
self::checkStatusCode($request['status_code']);// check the response validity
return true;
Expand All @@ -381,4 +410,3 @@ public function delete($options)
* @package PrestaShopWebservice
*/
class PrestaShopWebserviceException extends \Exception { }