iq2i/prestashop-webservice

A PHP library that let you interact with PrestaShop API

dev-main 2022-01-11 09:41 UTC

This package is auto-updated.

Last update: 2024-12-19 19:40:56 UTC


README

Continuous Integration Scrutinizer Code Quality Code Coverage

A PHP client library to interact with PrestaShop API.

Installation

composer require iq2i/prestashop-webservice

Basic usage

  1. Create a client instance:
use IQ2i\PrestashopWebservice\Http\Client;

$client = new Client([
    'url' => 'https://my-prestashop.com/api/',
    'key' => '6MBWZM37S6XCZXYT81GD6XD41SKZ14TP',
]);
  1. Create a request:
use IQ2i\PrestashopWebservice\Http\Request\GetRequest;

/** GET /api/categories/1 */
$request = new GetRequest('categories', 1);
  1. Execute request
$response = $client->execute($request);
  1. Use client's response:
$statusCode = $response->getStatusCode();
$header = $response->getHeaders();
$content = $response->getContent();

The response's content is an array, client automatically decode XML.

A request for each action

use IQ2i\PrestashopWebservice\Http\QueryAttribute\Schema;
use IQ2i\PrestashopWebservice\Http\Request\SchemaRequest;

/** GET /api/categories?schema=synopsis */
$request = new SchemaRequest('categories');
$request->addQueryAttribute(new Schema(Schema::SYNOPSIS))
use IQ2i\PrestashopWebservice\Http\Request\ListRequest;

/** GET /api/categories */
$request = new ListRequest('categories');
use IQ2i\PrestashopWebservice\Http\Request\GetRequest;

/** GET /api/categories/1 */
$request = new GetRequest('categories', 1);
use IQ2i\PrestashopWebservice\Http\Request\CreateRequest;

/** POST /api/categories */
$request = new CreateRequest('categories');
$request->setBody('XML content');
use IQ2i\PrestashopWebservice\Http\Request\UpdateRequest;

/** PUT /api/categories/1 */
$request = new UpdateRequest('categories', 1);
$request->setBody('XML content');
use IQ2i\PrestashopWebservice\Http\Request\DeleteRequest;

/** DELETE /api/categories/1 */
$request = new DeleteRequest('categories', 1);

Use query attributes

PrestaShop defines list options for webservice: https://devdocs.prestashop.com/1.7/webservice/cheat-sheet/#list-options

These options are available in this library:

use IQ2i\PrestashopWebservice\Http\QueryAttribute\Schema;

/** GET /api/categories?schema=synopsis */
$request = new SchemaRequest('categories');
$request->addQueryAttribute(new Schema(Schema::SYNOPSIS));
use IQ2i\PrestashopWebservice\Http\QueryAttribute\Filter;
use IQ2i\PrestashopWebservice\Http\QueryAttribute\Sort;

/** GET /api/categories?filter[name]=%[clothes]&sort=[name_ASC] */
$request = new ListRequest('categories');
$request->addQueryAttribute(new Filter('name', 'clothes', Filter::END));
$request->addQueryAttribute(new Sort('name', Sort::ASC));

Issues and feature requests

Please report issues and request features at https://github.com/iq2i/prestashop-webservice/issues.

License

This bundle is under the MIT license. For the whole copyright, see the LICENSE file distributed with this source code.