Magento 2: How to get order information using REST API?
For order information:
$userData = array("username" => "admin", "password" => "admin123"); $ch = curl_init("http://magento213/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)))); $token = curl_exec($ch); $ch = curl_init("http://magento213/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);
Api List
fetch all pending orders through REST API, add comments on it and change status for those orders
$userData = array("username" => "adminuser", "password" => "adminpassowrd");
$ch = curl_init("https://yourhost.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-Lenght: " . strlen(json_encode($userData))));
$token = curl_exec($ch);
$apiOrderUrl = 'https://yourhost.com/index.php/rest/V1/orders/?searchCriteria[filterGroups][0][filters][0][field]=status&searchCriteria[filterGroups][0][filters][0][value]=pending&searchCriteria[filterGroups][0][filters][0][conditionType]=eq';
$ch = curl_init($apiOrderUrl);
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);
$results = json_decode($result, true); // all orders with status pending
foreach ($results['items'] as $order) {
$commentData = array(
'id' => $order['entity_id'], //order_id
'statusHistory' => array(
'comment' => 'Received order?',
'entity_id' => null,
'is_customer_notified' => '1',
'created_at' => now(),
'parent_id' => $order['entity_id'], //order_id
'entity_name' => 'order',
'status' => 'processing', //assign new status to order
'is_visible_on_front' => '1'
)
);
$commentData = json_encode($commentData,true);
$orderStatusApiUrl = 'https://yourhost.com/index.php/rest/V1/orders/'.$order['entity_id'].'/comments';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$orderStatusApiUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $commentData );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
$response = curl_exec($ch); //true
}
Go to http://devdocs.magento.com/swagger/
salesOrderRepositoryV1 and customerCustomerRepositoryV1
Order info: GET: '/V1/orders/{id}'
Customer info: GET: '/V1/customers/{customerId}'
We can read more here: https://magento.stackexchange.com/a/149927/33057