Skip to content

Fix webservice call #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]"
"name": "Anglerfox",
"email": "[email protected]"
}
],
"autoload": {
Expand All @@ -30,7 +30,7 @@
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.2",
"Orchestra/Testbench": "^3.5"
"orchestra/testbenc": "^3.5"
},
"scripts": {
"test": "vendor/bin/phpunit",
Expand Down
10 changes: 7 additions & 3 deletions src/Exceptions/PrestashopWebServiceRequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
55 changes: 34 additions & 21 deletions src/PrestashopWebServiceLibrary.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Protechstudio\PrestashopWebService\Exceptions\PrestashopWebServiceException;
use Protechstudio\PrestashopWebService\Exceptions\PrestashopWebServiceRequestException;
use Illuminate\Support\Facades\Cache;
use SimpleXMLElement;

/**
Expand All @@ -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
Expand Down Expand Up @@ -63,7 +64,7 @@ public function __construct($url, $key, $debug = true)
$this->key = $key;
$this->debug = $debug;
$this->version = 'unknown';

$this->runningInConsole = app()->runningInConsole();
}

Expand Down Expand Up @@ -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']);
}
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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']);
Expand All @@ -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
);
}

Expand Down Expand Up @@ -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'];
Expand All @@ -313,7 +317,7 @@ public function add($options)
/**
* Retrieve (GET) a resource
* <p>Unique parameter must take : <br><br>
* 'url' => Full URL for a GET request of WebService (ex: http://mystore.com/api/customers/1/)<br>
* 'url' => Full URL for a GET request of WebService (ex: http://mystore.com//webservice/dispatcher.php?url=customers/1)<br>
* OR<br>
* 'resource' => Resource name,<br>
* 'id' => ID of a resource you want to get<br><br>
Expand Down Expand Up @@ -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'];
Expand All @@ -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']);
}

Expand All @@ -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'];
Expand All @@ -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');
Expand Down Expand Up @@ -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'])) {
Expand Down Expand Up @@ -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'])) {
Expand Down