From f2522cc50618b15a4fff3d5cda014b657e4dc82a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Nadaud?= Date: Wed, 26 Aug 2015 09:20:46 +0200 Subject: [PATCH] // Update PrestaShop compatibility --- PSWebServiceLibrary.php | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/PSWebServiceLibrary.php b/PSWebServiceLibrary.php index 5b50abc..b239f63 100644 --- a/PSWebServiceLibrary.php +++ b/PSWebServiceLibrary.php @@ -34,20 +34,20 @@ class PrestaShopWebservice /** @var string Shop URL */ protected $url; - + /** @var string Authentification key */ protected $key; - + /** @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.0'; - const psCompatibleVersionsMax = '1.6.1.0'; - + const psCompatibleVersionsMax = '1.6.1.1'; + /** * PrestaShopWebservice constructor. Throw an exception when CURL is not installed/activated * @@ -76,7 +76,7 @@ function __construct($url, $key, $debug = true) { $this->debug = $debug; $this->version = 'unknown'; } - + /** * Take the status code and throw an exception if the server didn't return 200 or 201 code * @param int $status_code Status code of an HTTP return @@ -112,7 +112,7 @@ protected function executeRequest($url, $curl_params = array()) CURLOPT_USERPWD => $this->key.':', CURLOPT_HTTPHEADER => array( 'Expect:' ) ); - + $session = curl_init($url); $curl_options = array(); @@ -133,12 +133,12 @@ protected function executeRequest($url, $curl_params = array()) $index = strpos($response, "\r\n\r\n"); if ($index === false && $curl_params[CURLOPT_CUSTOMREQUEST] != 'HEAD') throw new PrestaShopWebserviceException('Bad HTTP response'); - + $header = substr($response, 0, $index); $body = substr($response, $index + 4); - + $headerArrayTmp = explode("\n", $header); - + $headerArray = array(); foreach ($headerArrayTmp as &$headerItem) { @@ -147,7 +147,7 @@ protected function executeRequest($url, $curl_params = array()) if (count($tmp) == 2) $headerArray[$tmp[0]] = $tmp[1]; } - + if (array_key_exists('PSWS-Version', $headerArray)) { $this->version = $headerArray['PSWS-Version']; @@ -157,12 +157,12 @@ protected function executeRequest($url, $curl_params = array()) ) throw new PrestaShopWebserviceException('This library is not compatible with this version of PrestaShop. Please upgrade/downgrade this library'); } - + if ($this->debug) { $this->printDebug('HTTP REQUEST HEADER', curl_getinfo($session, CURLINFO_HEADER_OUT)); $this->printDebug('HTTP RESPONSE HEADER', $header); - + } $status_code = curl_getinfo($session, CURLINFO_HTTP_CODE); if ($status_code === 0) @@ -211,7 +211,7 @@ protected function parseXML($response) else throw new PrestaShopWebserviceException('HTTP response is empty'); } - + /** * Add (POST) a resource *

Unique parameter must take :

@@ -280,7 +280,7 @@ public function get($options) $url_params = array(); if (isset($options['id'])) $url .= '/'.$options['id']; - + $params = array('filter', 'display', 'sort', 'limit', 'id_shop', 'id_group_shop'); foreach ($params as $p) foreach ($options as $k => $o) @@ -291,9 +291,9 @@ public function get($options) } else throw new PrestaShopWebserviceException('Bad parameters given'); - + $request = self::executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'GET')); - + self::checkStatusCode($request['status_code']);// check the response validity return self::parseXML($request['response']); } @@ -314,7 +314,7 @@ public function head($options) $url_params = array(); if (isset($options['id'])) $url .= '/'.$options['id']; - + $params = array('filter', 'display', 'sort', 'limit'); foreach ($params as $p) foreach ($options as $k => $o) @@ -354,7 +354,7 @@ public function edit($options) } else throw new PrestaShopWebserviceException('Bad parameters given'); - + $request = self::executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'PUT', CURLOPT_POSTFIELDS => $xml)); self::checkStatusCode($request['status_code']);// check the response validity return self::parseXML($request['response']); @@ -400,7 +400,7 @@ public function delete($options) self::checkStatusCode($request['status_code']);// check the response validity return true; } - + } @@ -408,4 +408,3 @@ public function delete($options) * @package PrestaShopWebservice */ class PrestaShopWebserviceException extends Exception { } -