diff --git a/README.md b/README.md index 382f4b9..ada55b8 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Installation Require this package with composer using the following command: ```shell -composer require protechstudio/laravel-prestashop-webservice +composer require anglerfox/laravel-prestashop-webservice ``` After updating composer, add the service provider to the `providers` array in `config/app.php` diff --git a/composer.json b/composer.json index 36b3a39..5016de4 100644 --- a/composer.json +++ b/composer.json @@ -1,11 +1,11 @@ { - "name": "protechstudio/laravel-prestashop-webservice", + "name": "anglerfox/laravel-prestashop-webservice", "description": "Laravel 5 wrapper for Prestashop Web Service Library", "license": "MIT", "authors": [ { - "name": "Vito Laera", - "email": "vito.laera@protechstudio.it" + "name": "Anglerfox", + "email": "support@anglerfox.com" } ], "autoload": { @@ -30,7 +30,7 @@ }, "require-dev": { "squizlabs/php_codesniffer": "^3.2", - "Orchestra/Testbench": "^3.5" + "orchestra/testbenc": "^3.5" }, "scripts": { "test": "vendor/bin/phpunit", diff --git a/src/Exceptions/PrestashopWebServiceRequestException.php b/src/Exceptions/PrestashopWebServiceRequestException.php index 74ebea2..e456070 100644 --- a/src/Exceptions/PrestashopWebServiceRequestException.php +++ b/src/Exceptions/PrestashopWebServiceRequestException.php @@ -4,13 +4,17 @@ class PrestashopWebServiceRequestException extends PrestashopWebServiceException { - static protected $label = 'This call to PrestaShop Web Services failed and returned an HTTP status of %d. That means: %s.'; - protected $response; public function __construct($message = null, $code = null, $response = null) { - parent::__construct(sprintf(static::$label, $code, $message), $code); + if ($response instanceof \SimpleXMLElement && isset($response->errors->error->message)) { + $message = (string) $response->errors->error->message; + } + + $finalMessage = $message ?? sprintf("This call to PrestaShop Web Services failed with HTTP status %d.", $code); + + parent::__construct($finalMessage, $code); $this->response = $response; } diff --git a/src/PrestashopWebServiceLibrary.php b/src/PrestashopWebServiceLibrary.php index 160f2fa..0136296 100644 --- a/src/PrestashopWebServiceLibrary.php +++ b/src/PrestashopWebServiceLibrary.php @@ -4,6 +4,7 @@ use Protechstudio\PrestashopWebService\Exceptions\PrestashopWebServiceException; use Protechstudio\PrestashopWebService\Exceptions\PrestashopWebServiceRequestException; +use Illuminate\Support\Facades\Cache; use SimpleXMLElement; /** @@ -29,7 +30,7 @@ class PrestashopWebServiceLibrary /** @var array compatible versions of PrestaShop WebService */ const PS_COMPATIBLE_VERSION_MIN = '1.4.0.0'; - const PS_COMPATIBLE_VERSION_MAX = '1.7.99.99'; + const PS_COMPATIBLE_VERSION_MAX = '9.9.99.99'; /** * PrestaShopWebService constructor. Throw an exception when CURL is not installed/activated @@ -63,7 +64,7 @@ public function __construct($url, $key, $debug = true) $this->key = $key; $this->debug = $debug; $this->version = 'unknown'; - + $this->runningInConsole = app()->runningInConsole(); } @@ -93,12 +94,11 @@ protected function checkRequest($request) if ($request['response']) { $xml = $this->parseXML($request['response'], true); } - throw new PrestashopWebServiceRequestException($messages[$request['status_code']], $request['status_code'], $xml); } else { $exception = 'This call to PrestaShop Web Services returned an unexpected HTTP status of: '; $exception.= $request['status_code']; - throw new PrestashopWebServiceException($exception); + throw new PrestashopWebServiceRequestException($exception, $request['status_code'], null, $request['base_url']); } } @@ -151,8 +151,7 @@ protected function executeRequest($url, $curl_params = array()) $curl_options[$defkey] = $curl_params[$defkey]; } } - - list($response, $info, $error) = $this->executeCurl($url, $curl_options); + list($response, $info, $error) = $this->executeCurl($url.= '&ws_key=' . $this->key, $curl_options); $status_code = $info['http_code']; if ($status_code === 0 || $error) { @@ -166,6 +165,9 @@ protected function executeRequest($url, $curl_params = array()) $header = substr($response, 0, $index); $body = substr($response, $index); + $redirectUrl = isset($info['redirect_url']) ? $info['redirect_url'] : null; + $parsedUrl = parse_url($redirectUrl ?: $url); + $baseUrl = $parsedUrl['scheme'] . '://' . $parsedUrl['host']; $headerArray = array(); foreach (explode("\n", $header) as $headerItem) { @@ -177,8 +179,9 @@ protected function executeRequest($url, $curl_params = array()) } if (array_key_exists('PSWS-Version', $headerArray)) { - $this->isPrestashopVersionSupported($headerArray['PSWS-Version']); - $this->version = $headerArray['PSWS-Version']; + $version = $headerArray['PSWS-Version'] == 0 ? '1.7.5.1' : $headerArray['PSWS-Version']; + $this->isPrestashopVersionSupported($version); + $this->version = $version; } $this->printDebug('HTTP REQUEST HEADER', $info['request_header']); @@ -195,7 +198,8 @@ protected function executeRequest($url, $curl_params = array()) 'status_code' => $status_code, 'response' => $body, 'header' => $header, - 'headers' => $headerArray + 'headers' => $headerArray, + 'base_url' => $baseUrl ); } @@ -293,7 +297,7 @@ public function add($options) $xml = ''; if (isset($options['resource'], $options['postXml']) || isset($options['url'], $options['postXml'])) { - $url = (isset($options['resource']) ? $this->url.'/api/'.$options['resource'] : $options['url']); + $url = (isset($options['resource']) ? $this->url.'/webservice/dispatcher.php?url='.$options['resource'] : $options['url']); $xml = $options['postXml']; if (isset($options['id_shop'])) { $url .= '&id_shop='.$options['id_shop']; @@ -313,7 +317,7 @@ public function add($options) /** * Retrieve (GET) a resource *

Unique parameter must take :

- * 'url' => Full URL for a GET request of WebService (ex: http://mystore.com/api/customers/1/)
+ * 'url' => Full URL for a GET request of WebService (ex: http://mystore.com//webservice/dispatcher.php?url=customers/1)
* OR
* 'resource' => Resource name,
* 'id' => ID of a resource you want to get

@@ -344,7 +348,7 @@ public function get($options) if (isset($options['url'])) { $url = $options['url']; } elseif (isset($options['resource'])) { - $url = $this->url.'/api/'.$options['resource']; + $url = $this->url.'/webservice/dispatcher.php?url='.$options['resource']; $url_params = array(); if (isset($options['id'])) { $url .= '/'.$options['id']; @@ -359,15 +363,24 @@ public function get($options) } } if (count($url_params) > 0) { - $url .= '?'.http_build_query($url_params); + $url .= '&'.http_build_query($url_params); } } else { throw new PrestashopWebServiceException('Bad parameters given'); } - $request = $this->executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'GET')); - - $this->checkRequest($request);// check the response validity + if(isset($options['cache_key']) && !empty($options['cache_key'])){ + if(Cache::get($options['cache_key'])){ + $request['response'] = Cache::get($options['cache_key']); + }else{ + $request = $this->executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'GET')); + $this->checkRequest($request);// check the response validity + Cache::put($options['cache_key'], $request['response'] , $options['expiration'] ?? 86400); + } + }else{ + $request = $this->executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'GET')); + $this->checkRequest($request);// check the response validity + } return $this->parseXML($request['response']); } @@ -383,7 +396,7 @@ public function head($options) if (isset($options['url'])) { $url = $options['url']; } elseif (isset($options['resource'])) { - $url = $this->url.'/api/'.$options['resource']; + $url = $this->url.'/webservice/dispatcher.php?url='.$options['resource']; $url_params = array(); if (isset($options['id'])) { $url .= '/'.$options['id']; @@ -398,7 +411,7 @@ public function head($options) } } if (count($url_params) > 0) { - $url .= '?'.http_build_query($url_params); + $url .= '&'.http_build_query($url_params); } } else { throw new PrestashopWebServiceException('Bad parameters given'); @@ -428,7 +441,7 @@ public function edit($options) if (isset($options['url'])) { $url = $options['url']; } else { - $url = $this->url.'/api/'.$options['resource'].'/'.$options['id']; + $url = $this->url.'/webservice/dispatcher.php?url='.$options['resource'].'/'.$options['id']; } $xml = $options['putXml']; if (isset($options['id_shop'])) { @@ -476,9 +489,9 @@ public function delete($options) $url = $options['url']; } elseif (isset($options['resource']) && isset($options['id'])) { if (is_array($options['id'])) { - $url = $this->url.'/api/'.$options['resource'].'/?id=['.implode(',', $options['id']).']'; + $url = $this->url.'/webservice/dispatcher.php?url='.$options['resource'].'/?id=['.implode(',', $options['id']).']'; } else { - $url = $this->url.'/api/'.$options['resource'].'/'.$options['id']; + $url = $this->url.'/webservice/dispatcher.php?url='.$options['resource'].'/'.$options['id']; } } if (isset($options['id_shop'])) {