Download the PHP package retrowaver/allegro-rest-api-v2 without Composer
On this page you can find all versions of the php package retrowaver/allegro-rest-api-v2. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download retrowaver/allegro-rest-api-v2
More information about retrowaver/allegro-rest-api-v2
Files in retrowaver/allegro-rest-api-v2
Package allegro-rest-api-v2
Short Description Second version of a simple interface for Allegro REST API resources. Built on top of https://github.com/Wiatrogon/php-allegro-rest-api, which was rewritten for the most part.
License MIT
Informations about the package allegro-rest-api-v2
php-allegro-rest-api-v2
Second version of a simple interface for Allegro REST API resources. Built on top of https://github.com/Wiatrogon/php-allegro-rest-api, which was rewritten for the most part.
Main differences are:
- uses HTTPlug / PSR-7 for HTTP communication
- contains previously missing features (image upload)
- both
authorization code
andclient credentials
flows are supported (device flow
could be also easily implemented, as authorization is now separated from main API component) - is flexible (custom headers, custom middleware)
0. Installation
PHP Allegro REST API uses HTTPlug, and...
If a library depends on HTTPlug, it requires the virtual package
php-http/client-implementation
. A virtual package is used to declare that the library needs an implementation of the HTTPlug interfaces, but does not care which implementation specifically.
I recommend guzzle6 adapter, so run the following command before installing the PHP Allegro REST API library itself:
Now you're ready to run:
Note that you don't need to inject HTTP client and message factory into API client - HTTPlug uses discovery
to find implementations automatically.
If you want to pass client / message factory explicitly to the API client, you can still do that (see Api.php
constructor method).
1. Registering your app
In order to use Allegro REST API, you have to register your application (if you haven't done that already - see https://developer.allegro.pl/auth/#app).
2. Authorization
First, you need to get an authorization token. There are two different token managers provided for different types of authorization flows. Use one of them to get a token.
2.1. Authorization code flow
Learn how it works here: https://developer.allegro.pl/auth/#user
To use it with PHP Allegro REST API, do the following:
2.2. Client credentials flow
Client credentials flow doesn't require a user's permission. It's mostly used for accessing public data (searching offers, getting categories tree). Read more here: https://developer.allegro.pl/auth/#clientCredentialsFlow
2.3. Device flow
Device flow isn't supported right now. But you could always write your token manager and use the received token with API as usual. Read more here: https://developer.allegro.pl/auth/#DeviceFlow
3. Refreshing tokens
Authorization code tokens can be refreshed (see https://developer.allegro.pl/auth/#refresh-token).
4. Basic usage
4.1. Initializing
4.2. GET method
4.3. POST method
4.4. PUT method
4.5. DELETE method
4.6. Command
Some resources in API are only accesible using command pattern
(read more here https://developer.allegro.pl/command/).
In the example above, UUID is generated automatically. If you want, you can generate it yourself, and pass it as an argument:
5. Headers
You can alter headers being sent with your requests. You can do it on request basis, or once for all subsequent requests.
5.1. Why alter headers?
You may want to alter headers for several different reasons, mainly:
- if you want messages from API returned in Polish (add
Accept-Language: pl-PL
) - if you want to access beta methods (read more here https://developer.allegro.pl/about/#beta)
5.2. Send a single request with custom headers
5.3. Set custom headers for subsequent requests
5.3.1. Replacing headers
Note that there are some custom headers set by default (Content-Type: application/vnd.allegro.public.v1+json
and Accept: application/vnd.allegro.public.v1+json
). If you want to replace them, use setCustomHeaders()
:
5.3.2. Adding headers
If you want to add one or more custom headers, use addCustomHeaders()
:
Note that existing custom headers won't be replaced when using this method.
6. Middleware
PHP Allegro REST API has a middleware feature, that allows you to alter requests and responses any way you like.
At this point, there's only one 'real' built-in middleware - ImageUploadMiddleware
that alters request's URI in case of image upload request (from standard api.allegro.pl
to image upload-specific upload.allegro.pl
).
6.1. Create your own middleware
You can create your own middleware by creating a class implementing Retrowaver\Allegro\REST\Middleware\MiddlewareInterface
and passing it into API constructor:
7. Other stuff
7.1. Sandbox
If you want to use API with sandbox environment, use Sandbox
instead of Api
, SandboxAuthorizationCodeTokenManager
instead of AuthorizationCodeTokenManager
and SandboxClientCredentialsTokenManager
instead of ClientCredentialsTokenManager
.
7.2. Tests
7.2.1. Unit tests
Run unit tests with vendor/bin/phpunit tests --color
.
7.2.2. Sandbox tests
tests-sandbox
contains tests sending real HTTP requests to sandbox environment. If you want to run those, rename tests-sandbox/config.php.dist
to tests-sandbox/config.php
, insert your sandbox credentials, and then run vendor/bin/phpunit tests-sandbox --color
.
All versions of allegro-rest-api-v2 with dependencies
psr/http-message Version ^1.0
php-http/client-implementation Version ^1.0
php-http/httplug Version ^2.0
php-http/message-factory Version ^1.0
php-http/discovery Version ^1.0