eoko / magento2-client
Eoko Magento 2 client for the API
Installs: 2 827
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 3
Forks: 3
Open Issues: 0
Requires
- php: >=7.1
- php-http/client-implementation: ^1.0
- php-http/discovery: ^1.0
- php-http/httplug: ^1.0
- php-http/message: ^1.0
- php-http/message-factory: ^v1.0
- php-http/multipart-stream-builder: ^1.0
- psr/http-message: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^v2.3
- php-http/guzzle6-adapter: ^1.1
- phpspec/phpspec: 3.2.*
- phpunit/phpunit: 5.7.*
- symfony/yaml: ^3.3
Suggests
- php-http/guzzle6-adapter: In order to use Guzzle v6 as the HTTP client
This package is not auto-updated.
Last update: 2024-10-27 05:36:13 UTC
README
A simple PHP client to use the Magento2.
Requirements
- PHP >= 7.1
- Composer
Installation
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.
Getting started
Initialise the client
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);
Get a product
$product = $client->getProductApi()->get('top'); echo $product['sku']; // display "top"
Get a list of products
By getting pages
$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();
By getting a cursor
$products = $client->getProductApi()->all(50); foreach ($products as $product) { // do your stuff here echo $product['sku']; }
Create a product
unsupported
Update a product
$client->getProductApi()->update('top', ['family' => 'tshirt']);
Stock Item
Update a stock item
$api = $client->getProductApi()->getStockItemApi('MH03-M-Blue');
// There is nothing interesting in the output (product id :/)
$api->update($item['item_id'], ['qty' => 42]);
Support
If you find a bug or want to submit an improvement, don't hesitate to raise an issue on Github.