A simple PHP client to use the Magento2.
- PHP >= 7.1
- Composer
We use HTTPPlug as the HTTP client abstraction layer. In this example, we will use Guzzle v6 as the HTTP client implementation.
eoko/magento2-client
uses Composer.
The first step to use eoko/magento2-client
is to download composer:
$ curl -s http://getcomposer.org/installer | php
Then, run the following command to require the library:
$ php composer.phar require eoko/magento2-client php-http/guzzle6-adapter
If you want to use another HTTP client implementation, you can check here the full list of HTTP client implementations.
You first need to initialise the client with your credentials with admin token.
If you don't have any admin token, you can create it and retrieve with the following code :
<?php
require('./../vendor/autoload.php');
use Eoko\Magento2\Client\MagentoClientBuilder;
use Eoko\Magento2\Client\Security\AdminAuthentication;
// We initiate the client builder
$clientBuilder = new MagentoClientBuilder('http://m2.localhost:8000/rest/default');
// Create an unauthenticated client
$unAuthenticatedClient = $clientBuilder->buildAuthenticatedClient();
// Get an admin token
echo $unAuthenticatedClient->getAdminTokenApi()->getAdminToken('magento2', 'magento2');
After that, you can create an authenticated client :
<?php
require('./../vendor/autoload.php');
use Eoko\Magento2\Client\MagentoClientBuilder;
use Eoko\Magento2\Client\Security\AdminAuthentication;
$token = 'youtoken...';
// Authentication from admin token
$authentication = AdminAuthentication::fromAdminToken($token);
// Create an authenticated client
$authenticatedClient = $clientBuilder->buildAuthenticatedClient($authentication);
$product = $client->getProductApi()->get('top');
echo $product['sku']; // display "top"
$firstPage = $client->getProductApi()->listPerPage();
echo $page->getCount();
foreach ($page->getItems() as $product) {
// do your stuff here
echo $product['identifier'];
}
$nextPage = $page->getNextPage();
$firstPage = $nextPage->getPreviousPage();
$products = $client->getProductApi()->all(50);
foreach ($products as $product) {
// do your stuff here
echo $product['sku'];
}
unsupported
$client->getProductApi()->update('top', ['family' => 'tshirt']);
$api = $client->getProductApi()->getStockItemApi('MH03-M-Blue');
// There is nothing interesting in the output (product id :/)
$api->update($item['item_id'], ['qty' => 42]);
If you find a bug or want to submit an improvement, don't hesitate to raise an issue on Github.