Skip to content

Commit

Permalink
// Update PrestaShop compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jnadaud committed Aug 26, 2015
1 parent dc2c41e commit f2522cc
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions PSWebServiceLibrary.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <code>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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)
{
Expand All @@ -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'];
Expand All @@ -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)
Expand Down Expand Up @@ -211,7 +211,7 @@ protected function parseXML($response)
else
throw new PrestaShopWebserviceException('HTTP response is empty');
}

/**
* Add (POST) a resource
* <p>Unique parameter must take : <br><br>
Expand Down Expand Up @@ -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)
Expand All @@ -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']);
}
Expand All @@ -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)
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -400,12 +400,11 @@ public function delete($options)
self::checkStatusCode($request['status_code']);// check the response validity
return true;
}


}

/**
* @package PrestaShopWebservice
*/
class PrestaShopWebserviceException extends Exception { }

0 comments on commit f2522cc

Please sign in to comment.