|
| 1 | +# PHP Sylius PHP API client |
| 2 | + |
| 3 | +A simple PHP client to use the [Sylius PHP API](https://docs.sylius.com/en/latest/api/). |
| 4 | + |
| 5 | +Matrix compatibility: |
| 6 | + |
| 7 | +| Sylius version(s) | API PHP Client version |CI status | |
| 8 | +|--------------------|-------------------------|--------------------------------------------------------------------------------------------------------------------------| |
| 9 | +| v1.6 | v1.0 |[](https://travis-ci.org/akeneo/api-php-client)| |
| 10 | +| - | master |[](https://travis-ci.org/akeneo/api-php-client)| |
| 11 | + |
| 12 | +Note that our PHP client is backward compatible. |
| 13 | +For example, if your PIM is currently a v2.3, you can still use a 1.0 version of the PHP client. The new endpoints available in v2.3 will not be available in the v1.0 of the PHP client. |
| 14 | + |
| 15 | +## Requirements |
| 16 | + |
| 17 | +* PHP >= 5.6 |
| 18 | +* Composer |
| 19 | + |
| 20 | +## Installation |
| 21 | + |
| 22 | +We use HTTPPlug as the HTTP client abstraction layer. |
| 23 | +In this example, we will use [Guzzle](https://github.com/guzzle/guzzle) v6 as the HTTP client implementation. |
| 24 | + |
| 25 | +`api-php-client` uses [Composer](http://getcomposer.org). |
| 26 | +The first step to use `api-php-client` is to download composer: |
| 27 | + |
| 28 | +```bash |
| 29 | +$ curl -s http://getcomposer.org/installer | php |
| 30 | +``` |
| 31 | + |
| 32 | +Then, run the following command to require the library: |
| 33 | +```bash |
| 34 | +$ php composer.phar require akeneo/api-php-client php-http/guzzle6-adapter |
| 35 | +``` |
| 36 | + |
| 37 | +If you want to use another HTTP client implementation, you can check [here](https://packagist.org/providers/php-http/client-implementation) the full list of HTTP client implementations. |
| 38 | + |
| 39 | +## Documentation |
| 40 | + |
| 41 | +Full documentation is available on the [API website](https://api.akeneo.com/php-client/introduction.html). |
| 42 | + |
| 43 | +## Getting started |
| 44 | + |
| 45 | +### Initialise the client |
| 46 | +You first need to initialise the client with your credentials client id/secret and with your user/password. |
| 47 | + |
| 48 | +If you don't have any client id, let's take a look at [this page](https://api.akeneo.com/documentation/security.html#authentication) to create it. |
| 49 | + |
| 50 | +```php |
| 51 | +<?php |
| 52 | + |
| 53 | +require_once __DIR__ . '/vendor/autoload.php'; |
| 54 | + |
| 55 | +$clientBuilder = new \Akeneo\Pim\ApiClient\SyliusClientBuilder('http://localhost/'); |
| 56 | +$client = $clientBuilder->buildAuthenticatedByPassword('client_id', 'secret', 'admin', 'admin'); |
| 57 | +``` |
| 58 | + |
| 59 | +You can authenticate to the client with your token/refresh token as well. |
| 60 | +```php |
| 61 | +$client = $clientBuilder->buildAuthenticatedByToken('client_id', 'secret', 'token', 'refresh_token'); |
| 62 | +``` |
| 63 | + |
| 64 | +You can authenticate to the client with a custom header, in this case dummy client_id, secret, token and refresh token. |
| 65 | +```php |
| 66 | +$client = $clientBuilder->buildAuthenticatedByHeader(['X-AUTH-TOKEN' => 'qwertzuiopasdfghjkléxcvbnm']); |
| 67 | +``` |
| 68 | + |
| 69 | +Getting the token and refresh token is as simple as: |
| 70 | +```php |
| 71 | +$client->getToken(); |
| 72 | +$client->getRefreshToken(); |
| 73 | +``` |
| 74 | + |
| 75 | +### Get a product |
| 76 | + |
| 77 | +```php |
| 78 | +$product = $client->getProductApi()->get('top'); |
| 79 | +echo $product['identifier']; // display "top" |
| 80 | +``` |
| 81 | + |
| 82 | +### Get a list of products |
| 83 | + |
| 84 | +#### By getting pages |
| 85 | + |
| 86 | +```php |
| 87 | +$searchBuilder = new \Akeneo\Pim\ApiClient\Search\SearchBuilder(); |
| 88 | +$searchBuilder->addFilter('enabled', '=', true); |
| 89 | +$searchFilters = $searchBuilder->getFilters(); |
| 90 | + |
| 91 | +$firstPage = $client->getProductApi()->listPerPage(50, true, ['search' => $searchFilters]); |
| 92 | + |
| 93 | +echo $page->getCount(); |
| 94 | + |
| 95 | +foreach ($page->getItems() as $product) { |
| 96 | + // do your stuff here |
| 97 | + echo $product['identifier']; |
| 98 | +} |
| 99 | + |
| 100 | +$nextPage = $page->getNextPage(); |
| 101 | + |
| 102 | +$firstPage = $nextPage->getPreviousPage(); |
| 103 | +``` |
| 104 | + |
| 105 | +#### By getting a cursor |
| 106 | + |
| 107 | +```php |
| 108 | +$searchBuilder = new \Akeneo\Pim\ApiClient\Search\SearchBuilder(); |
| 109 | +$searchBuilder->addFilter('enabled', '=', true); |
| 110 | +$searchFilters = $searchBuilder->getFilters(); |
| 111 | + |
| 112 | +$products = $client->getProductApi()->all(50, ['search' => $searchFilters]); |
| 113 | +foreach ($products as $product) { |
| 114 | + // do your stuff here |
| 115 | + echo $product['identifier']; |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +### Create a product |
| 120 | + |
| 121 | +```php |
| 122 | +$client->getProductApi()->create('top', ['enabled' => true]); |
| 123 | +``` |
| 124 | + |
| 125 | +### Upsert a product |
| 126 | + |
| 127 | +```php |
| 128 | +$client->getProductApi()->upsert('top', ['family' => 'tshirt']); |
| 129 | +``` |
| 130 | + |
| 131 | +### Upsert a list of of products |
| 132 | + |
| 133 | +```php |
| 134 | +$client->getProductApi()->upsertList([ |
| 135 | + [ |
| 136 | + 'identifier' => 'top', |
| 137 | + 'family' => 'tshirt', |
| 138 | + ], |
| 139 | + [ |
| 140 | + 'identifier' => 'cap', |
| 141 | + 'categories' => ['hat'], |
| 142 | + ], |
| 143 | +]); |
| 144 | +``` |
| 145 | + |
| 146 | +## Testing |
| 147 | + |
| 148 | +Do note that you have to delete the `composer.lock` because Doctrine dependencies are loaded. |
| 149 | +These dependencies are different in function of the PHP version running `composer install`. |
| 150 | + |
| 151 | +``` |
| 152 | +cp docker-compose.yml.dist docker-compose.yml |
| 153 | +rm -rf composer.lock vendor/ |
| 154 | +docker-compose run client_56 composer install |
| 155 | +docker-compose run client_56 bin/phpunit -c phpunit.xml.dist |
| 156 | +docker-compose run client_56 bin/phpspec run |
| 157 | +docker-compose run client_56 bin/php-cs-fixer fix --diff --dry-run --config=.php_cs.php -vvv |
| 158 | +``` |
| 159 | + |
| 160 | +## Support |
| 161 | + |
| 162 | +The support of this client is made in best effort by our Akeneo team. |
| 163 | + |
| 164 | +If you find a bug or want to submit an improvement, don't hesitate to raise an issue on Github. |
| 165 | +Also, you can ask questions and discuss about the PHP client with the community in the [Slack User Group](https://akeneopim-ug.slack.com/messages/web-api/). |
0 commit comments