palpalani/magento2-rest-api-client

Fund package maintenance!
palPalani

Installs: 18 183

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 2

Forks: 0

v1.2.1 2022-03-16 13:42 UTC

This package is auto-updated.

Last update: 2024-10-29 14:44:50 UTC


README

Latest Version on Packagist GitHub Code Style Action Status Total Downloads

This is a simple PHP SDK lib to easily create Rest API.

Note: From the version 1.2 and above the Namespace was changed.

Installation

  1. Install it into your Magento 2 project with composer:
composer require palpalani/magento2-rest-api-client
  1. Enable module
bin/magento setup:upgrade

Example usage

<?php
require __DIR__ . '/vendor/autoload.php';

$service = new \Experius\Magento2ApiClient\Service\RestApi();
$service->setUsername('username');
$service->setPassword('password');
$service->setUrl('https://www.example.com/index.php/rest/%storecode/V1/');

// OPTIONAL > default = all
$service->setStoreCode('default');

$service->init();

Create product

$data = json_decode('
{
    "product": {
        "custom_attributes": [
                    {
                "attribute_code": "url_key",
                "value": "experius-example-product-new"
            }
        ],
        "name": "Experius Example Product",
        "weight": 1.2,
        "visibility": 4,
        "extension_attributes": {
            "website_ids": [
                "1"
            ],
            "stock_item": {
                "is_in_stock": true
            }
        },
        "sku": "experius-example-product",
        "status": 1,
        "type_id": "simple",
        "attribute_set_id": "4",
        "price": 10
    }
}');
$result = $service->call('products', $data, 'POST');
var_dump($result);

Update product

$data = json_decode('
{
    "product": {
        "custom_attributes": [
                    {
                "attribute_code": "url_key",
                "value": "experius-example-product-new"
            }
        ],
        "name": "Experius Example Product",
        "weight": 1.2,
        "visibility": 4,
        "extension_attributes": {
            "website_ids": [
                "1"
            ],
            "stock_item": {
                "is_in_stock": true
            }
        },
        "sku": "experius-example-product",
        "status": 1,
        "type_id": "simple",
        "attribute_set_id": "4",
        "price": 10
    }
}');
$result = $service->call('products/experius-example-product', $data, 'PUT');
var_dump($result);

Retrieve product

$result = $service->call('products/experius-example-product');
var_dump($result);

$dataArray = [
    'searchCriteria' => [
        'pageSize' => 10
    ]
];

$result = $service->call('products', $dataArray);
var_dump($result);

Testing

Unit Tests

vendor/bin/phpunit tests/unit

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.