Skip to main content

Get All Orders using REST Magento2

To get the all orders using RESt in Magento2, please follow these steps

1- Get the token using your backend credentail

url="http://www.magento.com/index.php/rest/";
$username="user";
$password='pass';

$token_url=$url."/V1/integration/admin/token";
//Authentication rest API magento2, get access token
$ch = curl_init();
$data = array("username" => $username, "password" => $password);
$data_string = json_encode($data);

$ch = curl_init($token_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// uncomment this code if you want to us the ssl (your URL is https)
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
    );
$token = curl_exec($ch);
$adminToken=  json_decode($token);

after you get the token you can now to get all orders by this code

$setHaders = array('Content-Type:application/json','Authorization:Bearer '.$adminToken);
$order_list= [
    "jsonData" =>'',
    "request_url" =>$url."/V1/orders?searchCriteria",
    "method" =>'GET',
];

$request = $order_list;
$jsonData = $request["jsonData"];
$request_url = $request["request_url"];
$method = $request["method"];

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $request_url);
if($jsonData!="") {
    curl_setopt($ch,CURLOPT_POSTFIELDS, $jsonData);
}
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_HTTPHEADER, $setHaders);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
print curl_exec($ch);
exit;$response = json_decode( curl_exec($ch), TRUE);
curl_close($ch);
var_export($response);