Question

I'm a Magento developer and I need to connect an app developed in Ionic with the Magento 2 REST API. I need to make an extension that grants more access to a customer token, additionally, I need to allow CRUD in custom tables created inside the Magento database. Anyone with some advice?

No correct solution

OTHER TIPS

REST API plays an important role in web apps and is one of the most popular API designs used recently. But for using REST API in Magento 2, you have to know the flow to call APIs in PHP.

In case you want to use token-based REST API in Magento 2, you will need authenticate, get the token then pass it in the header of every request you perform. Additionally, you also need to create a User Role web service then register the role to a new Admin User in Magento 2.

We will show you all the tutorials to help you use REST API in Magento 2.

  1. Create Web Service Role:
  2. Login Admin Panel> System> User Roles> Add New Role
  3. Add the Role Name and your current password of Admin in Your Password field
  4. Tap Role Resources
  5. Choose what are required for service of your web in Resource Access
  6. Tap Save Role
  7. Create Web Service User in Magento 2

This user is used for the role you’ve created

  1. Go to System> All Users> Add New User
  2. Fill in all the necessary information
  3. Tap User Role then choose which you’ve created
  4. Tap Save User

The user above will used to REST API web service in Magento 2.

Next, we will get starting with Magento 2 REST API.

Authenticate REST API through Token authentication:Use following code:

<?php

//API URL for authentication

$apiURL="http://example.com/rest/V1/integration/admin/token";

//parameters passing with URL

$data = array("username" => "apiaccess", "password" => "password");

$data_string = json_encode($data);

$ch = curl_init($apiURL);

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

//decoding generated token and saving it in a variable

$token=  json_decode($token); 

?>

This code will help pass username and password with API URL in order to authenticate Magento 2 REST API as well as saved the token.

*Note: The username and password are of what you’ve created

Get modules in Magento by REST API: By using REST API, you can get most of things in Magento 2. You can check in list of REST APIs for Magento EE and CE.

Use following code to demonstrate the API:

<?php

//Using above token into header

$headers = array("Authorization: Bearer ".$token);

//API URL to get all Magento 2 modules

$requestUrl='http://example.com/rest/V1/modules';

$ch = curl_init($requestUrl);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);

//decoding result

$result=  json_decode($result);

//printing result

print_r($result);

?>

With the code above, you passed the token in the header with the API URL in order to get all the modules installed on Magento 2 store.

Use following code to print the result:

<?php

//API URL for authentication

$apiURL="http://example.com/rest/V1/integration/admin/token";

//parameters passing with URL

$data = array("username" => "apiaccess", "password" => "password");

$data_string = json_encode($data);

$ch = curl_init($apiURL);

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

//decoding generated token and saving it in a variable

$token=  json_decode($token);

//******************************************//

//Using above token into header

$headers = array("Authorization: Bearer ".$token);

//API URL to get all Magento 2 modules

$requestUrl='http://example.com/rest/V1/modules';

$ch = curl_init($requestUrl);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);

//decoding result

$result=  json_decode($result);

//printing result

print_r($result);

?>

We’ve brought you many of tutorials related to Magento 2 REST API. Hope that they are helpful for you.

source

Magento 2 REST APIs

Also for CRUD check Create CRUD For Custom Table

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top