Get Customer's Orders using Magento2 API REST

in this code you will see how to get all Customer's Orders from Magento2 using the REST API

 

<?php

$access = [
    "url" =>"http://www.example.com/index.php/rest/",
    "username"=>"{admin user}",
    "password" =>"{admin password}"

];

$url=$access["url"];
$username= $access["username"];
$password=$access["password"];

$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);
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);
$setHaders = array('Content-Type:application/json','Authorization:Bearer '.$adminToken);

$searchCriteria_array = [];
$searchCriteria_array[] ="searchCriteria[filter_groups][0][filters][0][field]=customer_email";
$searchCriteria_array[] ="searchCriteria[filter_groups][0][filters][0][value]={email}";
$searchCriteria_array[] ="searchCriteria[filter_groups][0][filters][0][condition_type]=eq";

$searchCriteria = implode ("&",$searchCriteria_array);
$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"];

//print_r($request);exit;
$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);

 

Please replace all variables between curly brackets with the correct information