Jul.03

How to Create Web Service Rest/Soap Role and User in Magento 2

1.Login to admin panel.

2.Click on System.

3.Click on User Role.

4.Click on User Role “Add New Role”.

5.Set Name as something like “Web Service Role”.

6.Click on “Role Resources”.

7.Set only those resources that are required for web service.

8.Click Save Role.

9.Now create user for the role we have just created.

10.Click on System.

11.Click on “All Users”.

12.Click on “Add New User”.

13.Set users fields like User Name,First name,lastname, password etc..

14.Click on “User Role” Tab.

15.Select the newly created role.

16.Click “Save User”.

17.If it ask for clear cache then clear it.

 

Magento

Jul.03

Magento 2 Rest API example

Creating and using rest api in magento 2 is very easy but for that you need some startup example.Magento 2 uses token based rest api.First you need to authenticate user and get the token from magento 2.After getting token you have to pass this token to every request you performed.For example to get product details by SKU you need to first authenticate user and get token now pass this token to your  header to call web service  /V1/products/:sku

Here I have created example for get product by SKU using magento 2 rest api. Here is list of API that supported by magento 2. LIST OF Methods REST API MAGENTO 2

Please note that you need to create web service user/role it is not a good idea to use admin user into webservice.See here for how to create and use web service user in magento 2

Magento 2 REST API Authentication

//Authentication rest API magento2.Please change url accordingly your url
$adminUrl='http://127.0.0.1/magento2/index.php/rest/V1/integration/admin/token';
$ch = curl_init();
$data = array("username" => "wsuser", "password" => "password123");                                                                    
$data_string = json_encode($data);                       
$ch = curl_init($adminUrl); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);       
$token = curl_exec($ch);
$token=  json_decode($token);  

Get Product By SKU REST API Magento 2

//Use above token into header
$headers = array("Authorization: Bearer $token"); 

$requestUrl='http://127.0.0.1/magento2/index.php/rest/V1/products/24-MB01';
//Please note 24-MB01 is sku

$ch = curl_init();
$ch = curl_init($requestUrl); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   

$result = curl_exec($ch);
$result=  json_decode($result);
print_r($result);

 

PHP,Magento