Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,156,411 members, 7,830,069 topics. Date: Thursday, 16 May 2024 at 03:50 PM

How To Develop Android And iOS Mobile App In Magento2 With Rest API - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / How To Develop Android And iOS Mobile App In Magento2 With Rest API (1872 Views)

IOS Mobile App Developer Wanted / How Much Will You Develop Android App For Me / Ios Mobile App Deveveloper Needed (2) (3) (4)

(1) (Reply)

How To Develop Android And iOS Mobile App In Magento2 With Rest API by ilyasmuy: 12:27pm On Sep 27, 2017
With magento rest API, you can easily develop an API for your mobile app (Android, iOS et.c)
Magento 2 API framework allows developers to create new services for communicating with Magento 2 stores. It supports REST and SOAP web services and is based on CRUD operations (Create, Read, Update, Delete) and a Search Model.

Given the importance of REST API in web apps, It is surprising that a lot of Magento developers ask the question, What is Magento 2 REST API?

At the moment, Magento 2 uses the following three authentication methods:

OAuth 1.0a authentication for third-party applications.
Tokens to authenticate mobile applications.
Admins and customers authentication with login credentials.
These authentication methods can only access the resources assigned to them. Magento 2 API framework first checks whether the call has appropriate authorization to perform the request. The API framework also supports field filtering of API responses to preserve cellular bandwidth.

Developers use Magento 2 APIs for a wide range of tasks. For instance, you can create a shopping app and integrate it with your Magento 2 store. You can also build a web app which your employee could use to help customers make purchases. With the help of APIs, you can integrate your Magento 2 store with CRMs, ERPs or POS systems.

Using REST API in Magento 2


Using REST API in Magento 2 is a piece of cake. But for that, you need to understand the flow to call APIs in PHP.

If you want to use token-based Magento 2 REST API, first you will need to authenticate and get the token from Magento 2. Then, you will have to pass it in the header of every request you perform.

To get started with the REST API in Magento 2 using token-based authentication, you will need to create a web service User Role and register that role to a new Magento 2 Admin User. Keep in mind that creating a new role and user is necessary because it’s not a good practice to use Magento Owner User in a web service.

For the purpose of this tutorial, I will also use this REST API to get all the modules installed on your Magento 2 store.

Create Web Service Role in Magento 2

To create a web service role in Magento 2, follow these steps:

Login to the Magento 2 Admin Panel.
Go to System >> User Roles and tap the Add New Role
Enter the Role Name.
In Your Password field, enter the current password of your Magento 2 Admin.
Now, on the left side, click Role Resources.
In the Resource Access, select only those that are required for your web service.
Once done, hit the Save Role
Create Web Service User in Magento 2

Now, create a new user for the newly created role through these steps:

Go to System >> All Users and hit the Add New User
Enter the required information including User Name, First and Last Name, Email, Password, etc.
Now, on the left side, click User Role and select the newly created role.
Once done, click the Save User
That’s all; now I will use this user to the Magento 2 REST API web service.

Magento 2 REST API Authentication

As I mentioned earlier, I will authenticate REST API through Token authentication. This means that I will pass a username and password in the initial connection and receive the token . This token will be saved in a variable, which will be passed in the header for further calls.

Here is the code:


<?php

//API URL for authentication
$apiURL="http://magento-ilyas.cloudwaysapps.com/index.php/rest/V1/integration/admin/token";

//parameters passing with URL
$data = array("username" => "ilyasusernameaccess", "password" => "ilyaspassword);
$data_string = json_encode($data);

$ch = curl_init($apiURL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"wink;
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);

?>

In the above code, I passed username and password with the API URL to authenticate Magento 2 REST API and then saved the token in a variable for further use. Remember, you have to use the username password of the new user (created earlier).

Get Modules Using REST API in Magento 2

You can fetch almost everything using Magento 2 REST API. The List of REST APIs are here http://devdocs.magento.com/guides/v2.0/rest/list.html.

To demonstrate the API, I am going to get all the installed modules on a Magento 2 store. Here is the script:


<?php

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

//API URL to get all Magento 2 modules
$requestUrl='http://magento-ilyas.com/index.php/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);

?>


Once you execute this code it will show all module in the system


Conclusion

This is a very broad topic that requires a series of tutorial for complete coverage. The goal of this tutorial was to provide a simple example and demonstrate how easy it is to use REST API for Magento 2. I hope this tutorial proves useful for you and you could answer the question What is Magento 2 REST API? Feel free to get in touch if you need any help with the topic @ ilyas.muy@gmail.com

I am Ilyas, A magento certified developer with over 5 years experience. You can get in touch for your magento development @ ilyas.muy@gmail.com

Reference: https://www.cloudways.com/blog/magento-2-rest-api/

(1) (Reply)

Is It Good To Use Ms Access For A Company Database / Advice Welcomed - We're Putting A Team Of Devops Pro / Crystal Report In Vb.net

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 16
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.