dolibarr  7.0.0-beta
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  if (!extension_loaded('curl'))
74  throw new PrestaShopWebserviceException('Please activate the PHP extension \'curl\' to allow use of PrestaShop webservice library');
75  $this->url = $url;
76  $this->key = $key;
77  $this->debug = $debug;
78  $this->version = 'unknown';
79  }
80 
86  protected function checkStatusCode($status_code)
87  {
88  $error_label = 'This call to PrestaShop Web Services failed and returned an HTTP status of %d. That means: %s.';
89  switch($status_code)
90  {
91  case 200:
92  case 201:
93  break;
94  case 204:
95  throw new PrestaShopWebserviceException(sprintf($error_label, $status_code, 'No content'));
96  case 400:
97  throw new PrestaShopWebserviceException(sprintf($error_label, $status_code, 'Bad Request'));
98  case 401:
99  throw new PrestaShopWebserviceException(sprintf($error_label, $status_code, 'Unauthorized'));
100  case 404:
101  throw new PrestaShopWebserviceException(sprintf($error_label, $status_code, 'Not Found'));
102  case 405:
103  throw new PrestaShopWebserviceException(sprintf($error_label, $status_code, 'Method Not Allowed'));
104  case 500:
105  throw new PrestaShopWebserviceException(sprintf($error_label, $status_code, 'Internal Server Error'));
106  default:
107  throw new PrestaShopWebserviceException('This call to PrestaShop Web Services returned an unexpected HTTP status of:' . $status_code);
108  }
109  }
110 
118  public function executeRequest($url, $curl_params = array())
119  {
120  $defaultParams = array(
121  CURLOPT_HEADER => TRUE,
122  CURLOPT_RETURNTRANSFER => TRUE,
123  CURLINFO_HEADER_OUT => TRUE,
124  CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
125  CURLOPT_USERPWD => $this->key.':',
126  CURLOPT_HTTPHEADER => array( 'Expect:' )
127  );
128 
129  $session = curl_init($url);
130 
131  $curl_options = array();
132  foreach ($defaultParams as $defkey => $defval)
133  {
134  if (isset($curl_params[$defkey]))
135  $curl_options[$defkey] = $curl_params[$defkey];
136  else
137  $curl_options[$defkey] = $defaultParams[$defkey];
138  }
139  foreach ($curl_params as $defkey => $defval)
140  if (!isset($curl_options[$defkey]))
141  $curl_options[$defkey] = $curl_params[$defkey];
142 
143  curl_setopt_array($session, $curl_options);
144  $response = curl_exec($session);
145 
146  $index = strpos($response, "\r\n\r\n");
147  if ($index === false && $curl_params[CURLOPT_CUSTOMREQUEST] != 'HEAD')
148  throw new PrestaShopWebserviceException('Bad HTTP response');
149 
150  $header = substr($response, 0, $index);
151  $body = substr($response, $index + 4);
152 
153  $headerArrayTmp = explode("\n", $header);
154 
155  $headerArray = array();
156  foreach ($headerArrayTmp as &$headerItem)
157  {
158  $tmp = explode(':', $headerItem);
159  $tmp = array_map('trim', $tmp);
160  if (count($tmp) == 2)
161  $headerArray[$tmp[0]] = $tmp[1];
162  }
163 
164  if (array_key_exists('PSWS-Version', $headerArray))
165  {
166  $this->version = $headerArray['PSWS-Version'];
167  if (
168  version_compare(PrestaShopWebservice::PSCOMPATIBLEVERSIONMIN, $headerArray['PSWS-Version']) == 1 ||
169  version_compare(PrestaShopWebservice::PSCOMPATIBLEVERSIONMAX, $headerArray['PSWS-Version']) == -1
170  )
171  throw new PrestaShopWebserviceException('This library is not compatible with this version of PrestaShop. Please upgrade/downgrade this library');
172  }
173 
174  if ($this->debug)
175  {
176  $this->printDebug('HTTP REQUEST HEADER', curl_getinfo($session, CURLINFO_HEADER_OUT));
177  $this->printDebug('HTTP RESPONSE HEADER', $header);
178 
179  }
180  $status_code = curl_getinfo($session, CURLINFO_HTTP_CODE);
181  if ($status_code === 0)
182  throw new PrestaShopWebserviceException('CURL Error: '.curl_error($session));
183  curl_close($session);
184  if ($this->debug)
185  {
186  if ($curl_params[CURLOPT_CUSTOMREQUEST] == 'PUT' || $curl_params[CURLOPT_CUSTOMREQUEST] == 'POST')
187  $this->printDebug('XML SENT', urldecode($curl_params[CURLOPT_POSTFIELDS]));
188  if ($curl_params[CURLOPT_CUSTOMREQUEST] != 'DELETE' && $curl_params[CURLOPT_CUSTOMREQUEST] != 'HEAD')
189  $this->printDebug('RETURN HTTP BODY', $body);
190  }
191  return array('status_code' => $status_code, 'response' => $body, 'header' => $header);
192  }
193 
201  public function printDebug($title, $content)
202  {
203  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>';
204  }
205 
211  public function getVersion()
212  {
213  return $this->version;
214  }
215 
222  protected function parseXML($response)
223  {
224  if ($response != '')
225  {
226  libxml_clear_errors();
227  libxml_use_internal_errors(true);
228  $xml = simplexml_load_string($response,'SimpleXMLElement', LIBXML_NOCDATA);
229  if (libxml_get_errors())
230  {
231  $msg = var_export(libxml_get_errors(), true);
232  libxml_clear_errors();
233  throw new PrestaShopWebserviceException('HTTP XML response is not parsable: '.$msg);
234  }
235  return $xml;
236  }
237  else
238  throw new PrestaShopWebserviceException('HTTP response is empty');
239  }
240 
251  public function add($options)
252  {
253  $xml = '';
254 
255  if (isset($options['resource'], $options['postXml']) || isset($options['url'], $options['postXml']))
256  {
257  $url = (isset($options['resource']) ? $this->url.'/api/'.$options['resource'] : $options['url']);
258  $xml = $options['postXml'];
259  if (isset($options['id_shop']))
260  $url .= '&id_shop='.$options['id_shop'];
261  if (isset($options['id_group_shop']))
262  $url .= '&id_group_shop='.$options['id_group_shop'];
263  }
264  else
265  throw new PrestaShopWebserviceException('Bad parameters given');
266  $request = self::executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => $xml));
267 
268  self::checkStatusCode($request['status_code']);
269  return self::parseXML($request['response']);
270  }
271 
300  public function get($options)
301  {
302  if (isset($options['url']))
303  $url = $options['url'];
304  elseif (isset($options['resource']))
305  {
306  $url = $this->url.'/api/'.$options['resource'];
307  $url_params = array();
308  if (isset($options['id']))
309  $url .= '/'.$options['id'];
310 
311  $params = array('filter', 'display', 'sort', 'limit', 'id_shop', 'id_group_shop');
312  foreach ($params as $p)
313  foreach ($options as $k => $o)
314  if (strpos($k, $p) !== false)
315  $url_params[$k] = $options[$k];
316  if (count($url_params) > 0)
317  $url .= '?'.http_build_query($url_params);
318  }
319  else
320  throw new PrestaShopWebserviceException('Bad parameters given ');
321 
322  $request = self::executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'GET'));
323  self::checkStatusCode($request['status_code']);// check the response validity
324  return self::parseXML($request['response']);
325  }
326 
333  public function head($options)
334  {
335  if (isset($options['url']))
336  $url = $options['url'];
337  elseif (isset($options['resource']))
338  {
339  $url = $this->url.'/api/'.$options['resource'];
340  $url_params = array();
341  if (isset($options['id']))
342  $url .= '/'.$options['id'];
343 
344  $params = array('filter', 'display', 'sort', 'limit');
345  foreach ($params as $p)
346  foreach ($options as $k => $o)
347  if (strpos($k, $p) !== false)
348  $url_params[$k] = $options[$k];
349  if (count($url_params) > 0)
350  $url .= '?'.http_build_query($url_params);
351  }
352  else
353  throw new PrestaShopWebserviceException('Bad parameters given');
354  $request = self::executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'HEAD', CURLOPT_NOBODY => true));
355  self::checkStatusCode($request['status_code']);// check the response validity
356  return $request['header'];
357  }
369  public function edit($options)
370  {
371  $xml = '';
372  if (isset($options['url']))
373  $url = $options['url'];
374  elseif ((isset($options['resource'], $options['id']) || isset($options['url'])) && $options['putXml'])
375  {
376  $url = (isset($options['url']) ? $options['url'] : $this->url.'/api/'.$options['resource'].'/'.$options['id']);
377  $xml = $options['putXml'];
378  if (isset($options['id_shop']))
379  $url .= '&id_shop='.$options['id_shop'];
380  if (isset($options['id_group_shop']))
381  $url .= '&id_group_shop='.$options['id_group_shop'];
382  }
383  else
384  throw new PrestaShopWebserviceException('Bad parameters given');
385 
386  $request = self::executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'PUT', CURLOPT_POSTFIELDS => $xml));
387  self::checkStatusCode($request['status_code']);// check the response validity
388  return self::parseXML($request['response']);
389  }
390 }
391 
396 {
397 
398 }
__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.
if($_POST["cancel"]==$langs->trans("Cancel")&&!$id) if($action== 'setdatev'&&$user->rights->tax->charges->creer) if($action== 'add'&&$_POST["cancel"]<> $langs->trans("Cancel")) if($action== 'delete') $title
Actions.
Definition: card.php:183
checkStatusCode($status_code)
Take the status code and throw an exception if the server didn'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.