Magento 2 REST API – Get All Orders

magento2rest api

How to GET all orders from magento 2 through Rest API. what is the endpoint url to retrieve all order irrespective of customers.

Best Answer

There a good tutorial about Magento 2 API Here

On the basis of above tutorial you can get all order by:

<?php
ini_set('error_reporting', E_ALL);
ini_set("display_errors", "1");

$userData = array("username" => "Admin_username", "password" => "Admin_password");
$ch = curl_init("http://127.0.0.1/mago/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-Lenght: " . strlen(json_encode($userData))));

echo $token = curl_exec($ch);

$ch = curl_init("http://127.0.0.1/mago/index.php/rest/V1/orders?searchCriteria=all");
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);

var_dump($result);

Note: searchCriteria=all and searchCriteria=0 both are giving same results

Related Topic