dolibarr  9.0.0
PSWebServiceLibrary.class.php
1 <?php
2 /*
3 * 2007-2013 PrestaShop
4 *
5 * NOTICE OF LICENSE
6 *
7 * This source file is subject to the Open Software License (OSL 3.0)
8 * that is bundled with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://opensource.org/licenses/osl-3.0.php
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@prestashop.com so we can send you a copy immediately.
14 *
15 * DISCLAIMER
16 *
17 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
18 * versions in the future. If you wish to customize PrestaShop for your
19 * needs please refer to http://www.prestashop.com for more information.
20 *
21 * @author PrestaShop SA <contact@prestashop.com>
22 * @copyright 2007-2013 PrestaShop SA
23 * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
24 * International Registered Trademark & Property of PrestaShop SA
25 * PrestaShop Webservice Library
26 * @package PrestaShopWebservice
27 */
28 
33 {
34 
36  protected $url;
37 
39  protected $key;
40 
42  protected $debug;
43 
45  protected $version;
46 
48  const PSCOMPATIBLEVERSIONMIN = '1.4.0.0';
49  const PSCOMPATIBLEVERSIONMAX = '1.6.99.99';
50 
51 
72  function __construct($url, $key, $debug = true)
73  {
74  if (!extension_loaded('curl'))
75  throw new PrestaShopWebserviceException('Please activate the PHP extension \'curl\' to allow use of PrestaShop webservice library');
76  $this->url = $url;
77  $this->key = $key;
78  $this->debug = $debug;
79  $this->version = 'unknown';
80  }
81 
88  protected function checkStatusCode($status_code)
89  {
90  $error_label = 'This call to PrestaShop Web Services failed and returned an HTTP status of %d. That means: %s.';
91  switch($status_code)
92  {
93  case 200:
94  case 201:
95  break;
96  case 204:
97  throw new PrestaShopWebserviceException(sprintf($error_label, $status_code, 'No content'));
98  case 400:
99  throw new PrestaShopWebserviceException(sprintf($error_label, $status_code, 'Bad Request'));
100  case 401:
101  throw new PrestaShopWebserviceException(sprintf($error_label, $status_code, 'Unauthorized'));
102  case 404:
103  throw new PrestaShopWebserviceException(sprintf($error_label, $status_code, 'Not Found'));
104  case 405:
105  throw new PrestaShopWebserviceException(sprintf($error_label, $status_code, 'Method Not Allowed'));
106  case 500:
107  throw new PrestaShopWebserviceException(sprintf($error_label, $status_code, 'Internal Server Error'));
108  default:
109  throw new PrestaShopWebserviceException('This call to PrestaShop Web Services returned an unexpected HTTP status of:' . $status_code);
110  }
111  }
112 
120  public function executeRequest($url, $curl_params = array())
121  {
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:' )
129  );
130 
131  $session = curl_init($url);
132 
133  $curl_options = array();
134  foreach ($defaultParams as $defkey => $defval)
135  {
136  if (isset($curl_params[$defkey]))
137  $curl_options[$defkey] = $curl_params[$defkey];
138  else
139  $curl_options[$defkey] = $defaultParams[$defkey];
140  }
141  foreach ($curl_params as $defkey => $defval)
142  if (!isset($curl_options[$defkey]))
143  $curl_options[$defkey] = $curl_params[$defkey];
144 
145  curl_setopt_array($session, $curl_options);
146  $response = curl_exec($session);
147 
148  $index = strpos($response, "\r\n\r\n");
149  if ($index === false && $curl_params[CURLOPT_CUSTOMREQUEST] != 'HEAD')
150  throw new PrestaShopWebserviceException('Bad HTTP response');
151 
152  $header = substr($response, 0, $index);
153  $body = substr($response, $index + 4);
154 
155  $headerArrayTmp = explode("\n", $header);
156 
157  $headerArray = array();
158  foreach ($headerArrayTmp as &$headerItem)
159  {
160  $tmp = explode(':', $headerItem);
161  $tmp = array_map('trim', $tmp);
162  if (count($tmp) == 2)
163  $headerArray[$tmp[0]] = $tmp[1];
164  }
165 
166  if (array_key_exists('PSWS-Version', $headerArray))
167  {
168  $this->version = $headerArray['PSWS-Version'];
169  if (
170  version_compare(PrestaShopWebservice::PSCOMPATIBLEVERSIONMIN, $headerArray['PSWS-Version']) == 1 ||
171  version_compare(PrestaShopWebservice::PSCOMPATIBLEVERSIONMAX, $headerArray['PSWS-Version']) == -1
172  )
173  throw new PrestaShopWebserviceException('This library is not compatible with this version of PrestaShop. Please upgrade/downgrade this library');
174  }
175 
176  if ($this->debug)
177  {
178  $this->printDebug('HTTP REQUEST HEADER', curl_getinfo($session, CURLINFO_HEADER_OUT));
179  $this->printDebug('HTTP RESPONSE HEADER', $header);
180  }
181  $status_code = curl_getinfo($session, CURLINFO_HTTP_CODE);
182  if ($status_code === 0)
183  throw new PrestaShopWebserviceException('CURL Error: '.curl_error($session));
184  curl_close($session);
185  if ($this->debug)
186  {
187  if ($curl_params[CURLOPT_CUSTOMREQUEST] == 'PUT' || $curl_params[CURLOPT_CUSTOMREQUEST] == 'POST')
188  $this->printDebug('XML SENT', urldecode($curl_params[CURLOPT_POSTFIELDS]));
189  if ($curl_params[CURLOPT_CUSTOMREQUEST] != 'DELETE' && $curl_params[CURLOPT_CUSTOMREQUEST] != 'HEAD')
190  $this->printDebug('RETURN HTTP BODY', $body);
191  }
192  return array('status_code' => $status_code, 'response' => $body, 'header' => $header);
193  }
194 
202  public function printDebug($title, $content)
203  {
204  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>';
205  }
206 
212  public function getVersion()
213  {
214  return $this->version;
215  }
216 
223  protected function parseXML($response)
224  {
225  if ($response != '')
226  {
227  libxml_clear_errors();
228  libxml_use_internal_errors(true);
229  $xml = simplexml_load_string($response,'SimpleXMLElement', LIBXML_NOCDATA);
230  if (libxml_get_errors())
231  {
232  $msg = var_export(libxml_get_errors(), true);
233  libxml_clear_errors();
234  throw new PrestaShopWebserviceException('HTTP XML response is not parsable: '.$msg);
235  }
236  return $xml;
237  }
238  else
239  throw new PrestaShopWebserviceException('HTTP response is empty');
240  }
241 
252  public function add($options)
253  {
254  $xml = '';
255 
256  if (isset($options['resource'], $options['postXml']) || isset($options['url'], $options['postXml']))
257  {
258  $url = (isset($options['resource']) ? $this->url.'/api/'.$options['resource'] : $options['url']);
259  $xml = $options['postXml'];
260  if (isset($options['id_shop']))
261  $url .= '&id_shop='.$options['id_shop'];
262  if (isset($options['id_group_shop']))
263  $url .= '&id_group_shop='.$options['id_group_shop'];
264  }
265  else
266  throw new PrestaShopWebserviceException('Bad parameters given');
267  $request = self::executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => $xml));
268 
269  self::checkStatusCode($request['status_code']);
270  return self::parseXML($request['response']);
271  }
272 
301  public function get($options)
302  {
303  if (isset($options['url']))
304  $url = $options['url'];
305  elseif (isset($options['resource']))
306  {
307  $url = $this->url.'/api/'.$options['resource'];
308  $url_params = array();
309  if (isset($options['id']))
310  $url .= '/'.$options['id'];
311 
312  $params = array('filter', 'display', 'sort', 'limit', 'id_shop', 'id_group_shop');
313  foreach ($params as $p)
314  foreach ($options as $k => $o)
315  if (strpos($k, $p) !== false)
316  $url_params[$k] = $options[$k];
317  if (count($url_params) > 0)
318  $url .= '?'.http_build_query($url_params);
319  }
320  else
321  throw new PrestaShopWebserviceException('Bad parameters given ');
322 
323  $request = self::executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'GET'));
324  self::checkStatusCode($request['status_code']);// check the response validity
325  return self::parseXML($request['response']);
326  }
327 
334  public function head($options)
335  {
336  if (isset($options['url']))
337  $url = $options['url'];
338  elseif (isset($options['resource']))
339  {
340  $url = $this->url.'/api/'.$options['resource'];
341  $url_params = array();
342  if (isset($options['id']))
343  $url .= '/'.$options['id'];
344 
345  $params = array('filter', 'display', 'sort', 'limit');
346  foreach ($params as $p)
347  foreach ($options as $k => $o)
348  if (strpos($k, $p) !== false)
349  $url_params[$k] = $options[$k];
350  if (count($url_params) > 0)
351  $url .= '?'.http_build_query($url_params);
352  }
353  else
354  throw new PrestaShopWebserviceException('Bad parameters given');
355  $request = self::executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'HEAD', CURLOPT_NOBODY => true));
356  self::checkStatusCode($request['status_code']);// check the response validity
357  return $request['header'];
358  }
370  public function edit($options)
371  {
372  $xml = '';
373  if (isset($options['url']))
374  $url = $options['url'];
375  elseif ((isset($options['resource'], $options['id']) || isset($options['url'])) && $options['putXml'])
376  {
377  $url = (isset($options['url']) ? $options['url'] : $this->url.'/api/'.$options['resource'].'/'.$options['id']);
378  $xml = $options['putXml'];
379  if (isset($options['id_shop']))
380  $url .= '&id_shop='.$options['id_shop'];
381  if (isset($options['id_group_shop']))
382  $url .= '&id_group_shop='.$options['id_group_shop'];
383  }
384  else
385  throw new PrestaShopWebserviceException('Bad parameters given');
386 
387  $request = self::executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'PUT', CURLOPT_POSTFIELDS => $xml));
388  self::checkStatusCode($request['status_code']);// check the response validity
389  return self::parseXML($request['response']);
390  }
391 }
392 
397 {
398 
399 }
__construct($url, $key, $debug=true)
PrestaShopWebservice constructor.
edit($options)
Edit (PUT) a resource.
executeRequest($url, $curl_params=array())
Handles a CURL request to PrestaShop Webservice.
printDebug($title, $content)
Output debug info.
checkStatusCode($status_code)
Take the status code and throw an exception if the server didn&#39;t return 200 or 201 code...
add($options)
Add (POST) a resource.
head($options)
Head method (HEAD) a resource.
parseXML($response)
Load XML from string.