2017 © Pedro Peláez
 

library magento2-client

Eoko Magento 2 client for the API

image

eoko/magento2-client

Eoko Magento 2 client for the API

  • Tuesday, February 27, 2018
  • by iam_merlin
  • Repository
  • 2 Watchers
  • 1 Stars
  • 366 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 9 Versions
  • 23 % Grown

The README.md

Eoko/magento2-client

Build Status, (*1)

A simple PHP client to use the Magento2., (*2)

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., (*3)

eoko/magento2-client uses Composer. The first step to use eoko/magento2-client is to download composer:, (*4)

$ curl -s http://getcomposer.org/installer | php

Then, run the following command to require the library:, (*5)

$ 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., (*6)

Getting started

Initialise the client

You first need to initialise the client with your credentials with admin token., (*7)

If you don't have any admin token, you can create it and retrieve with the following code :, (*8)

```php <?php, (*9)

require('./../vendor/autoload.php');, (*10)

use Eoko\Magento2\Client\MagentoClientBuilder; use Eoko\Magento2\Client\Security\AdminAuthentication;, (*11)

// We initiate the client builder $clientBuilder = new MagentoClientBuilder('http://m2.localhost:8000/rest/default');, (*12)

// Create an unauthenticated client $unAuthenticatedClient = $clientBuilder->buildAuthenticatedClient();, (*13)

// Get an admin token echo $unAuthenticatedClient->getAdminTokenApi()->getAdminToken('magento2', 'magento2'); ```, (*14)

After that, you can create an authenticated client :, (*15)

<?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, (*16)

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., (*17)

The Versions