bbcreation/allegro-api

Fork Allegro API client from ircykk - add support to Laravel 6

v1.22.0 2019-12-09 17:33 UTC

README

Allegro API client library, written with PHP.

Requirements

  • PHP >= 5.6 || PHP >= 7.0
  • HTTP client

Features

  • REST and SOAP WebApi
  • Sandbox support
  • Auto-complete
  • PSR compatible

Installation

Via Composer:

composer require ircykk/allegro-api

Library is build on top of HTTPlug, we need to install HTTP client.

composer require php-http/guzzle6-adapter "^1.1"

Developer Documentation

https://developer.allegro.pl/documentation/

Usage of client

Authentication with OAuth

<?php

// Composer autoload
require_once __DIR__.'/vendor/autoload.php';

$credentials = ...

$client = new \bbcreation\AllegroApi\Client($credentials);

// Redirect to allegro for authenticate and get back with code
if (!isset($_GET['code'])) {
    header('Location: '.$client->getAuthUrl());
} else {
    $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);

    // Store access token...
}

We have $token->access_token for authenticate all our future requests.

See example.

Client credentials flow

In order to access to public available resources such as categories or offers use client credentials flow:

$token = $client->fetchAccessTokenWithClientCredentials();

Device flow

To use browserless device flow use getAuthUserCode() method to get user_code and verification uri:

$code = $client->getAuthUserCode();

and then after user authenticate device you can fetch acces_token:

$code = ...

$token = $client->fetchAccessTokenWithDeviceCode($code->device_code);

Making Requests

<?php

// Composer autoload
require_once __DIR__.'/vendor/autoload.php';

$credentials = ...
$token = ...

$client = new \bbcreation\AllegroApi\Client($credentials);
$client->authenticate($token);

$categories = $client->sale()->categories()->all();

Making SOAP Requests

$credentials = ...

// WebApi SOAP client
$soapClient = new \bbcreation\AllegroApi\WebapiClient($credentials);

$categories = $soapClient->webApi()->getCatsDataLimit(0, 10);

Sandbox

In order to use Sandbox environment just set Credentials property $sandbox to true.

$credentials = new \bbcreation\AllegroApi\Credentials(
    ...
    true // Sandbox
);

Cache usage

Use any PSR-6 compatible library to cache requests.

In this example we use Symfony Cache, to install just run:

$ composer require symfony/cache
$credentials = ...
$client = new Client($credentials);

$cache = new FilesystemAdapter();
$client->addCache($cache, ['default_ttl' => 3600]);

See example.

Logger

Use any PSR-3 logger library for example Monolog, to install just run:

$ composer require monolog/monolog
$credentials = ...
$client = new Client($credentials);

$logger = new Logger('api');
$logger->pushHandler(
    new StreamHandler(__DIR__.'/api.log', Logger::DEBUG)
);
$loggerPlugin = new LoggerPlugin($logger);
$client->addPlugin($loggerPlugin);

See example.

Customization

Thanks to HTTPlug library can be customized easily, for example to set language use HeaderDefaultsPlugin plugin:

...
$headerDefaultsPlugin = new HeaderDefaultsPlugin([
    'Accept-Language' => 'en-US'
]);
$client->addPlugin($headerDefaultsPlugin);

See full list of available HTTPlug plugins.

TO-DO

  • Tests
  • Documentation

Contributing

Feel free to contribute.

Credits

API client build on top of HTTPlug and inspired by KnpLabs GitHub client.

Soap types generated by wsdl2phpgenerator library.