palpalani / magento2-rest-api-client
Fund package maintenance!
palPalani
Requires
- php: ^7.2|^8.0
- ext-curl: *
- ext-json: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpunit/phpunit: ^9.5
- vimeo/psalm: ^4.8
README
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
- Install it into your Magento 2 project with composer:
composer require palpalani/magento2-rest-api-client
- 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.