May 9, 2022
Magento 2 How To Get Order Information Using REST API
Hello Guys! 👋
In a previous blog, we learned about how to create a custom REST API, here is a link to check out that blog, now in this blog we learn how to get order information using REST API.
The REST API is a set of functions that call using HTTPS request and response.
Add the following code in PHP script anywhere on your server or localhost.
<?php
$adminData = array("username" => "admin", "password" => "admin@123");
$ch = curl_init("http://yoursite.com/index.php/rest/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Length: " . strlen(json_encode($userData))));
$token = curl_exec($ch);
$ch = curl_init("http://yoursite.com/index.php/rest/V1/orders/1");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
$result = curl_exec($ch);
$result = json_decode($result, 1);
echo '<pre>';
print_r($result);
Hope! It will help you
Thank you 😊