PHP code example of palpalani / magento2-rest-api-client

1. Go to this page and download the library: Download palpalani/magento2-rest-api-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

palpalani / magento2-rest-api-client example snippets



vice = 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();

$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);

$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);

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

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

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