8

I want to get order,customer information by using REST API.

Anyone have idea about this?

Khoa Truong
32.5k11 gold badges91 silver badges159 bronze badges
asked Jan 11, 2017 at 10:01
2
  • You want to get order, customer info using REST API? Commented Jan 11, 2017 at 10:02
  • Yes, I want order,customer info using REST API @KhoaTruongDinh Commented Jan 11, 2017 at 10:03

5 Answers 5

22

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-Length: " . 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

seanbreeden
2,82319 silver badges24 bronze badges
answered Jan 11, 2017 at 11:00
4
  • I am using same code but facing problem in getting password. For Password , $this->authSession->getUser()->getPassword(); but its returning encrypted password so rest api throw an error that you are not login properly. So, How to get decrypted password? Commented Jan 11, 2017 at 11:26
  • Password encryption is one way. So you can't decrypt. Commented Jan 11, 2017 at 11:30
  • But with encrypted password , I am not getting token here. So, How to get token with encrypted password ? Error: {"message":"You did not sign in correctly or your account is temporarily disabled."} Commented Jan 11, 2017 at 11:34
  • How can I create my magento api to use this code snippet ? Commented Apr 7, 2019 at 15:20
5

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

answered Jan 11, 2017 at 10:07
3
  • but how to implement REST api ? Commented Jan 11, 2017 at 10:21
  • Read my answer here: magento.stackexchange.com/a/149927/33057 Commented Jan 11, 2017 at 10:22
  • could you please tell what are the parameters in body for the post man ? Commented Oct 4, 2020 at 7:55
5

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
}
answered May 30, 2019 at 8:04
1

For Get Order Collection using REST API, You can pass below request url to fetch order data from Magento.

{SITE_URL}/rest/V1/orders?searchCriteria=entity_id&limit=5

Now based on above criteria you can get 5 order from order collection.

answered Nov 2, 2018 at 5:05
1
  • 2
    not work it return all result not only 5 records Commented May 7, 2020 at 16:30
0

For any reason if you need to fetch order collection, a search criteria has to be amended to the URL. If you need to fetch all the orders, please use empty search criteria.

Example : http://magento213/index.php/rest/V1/orders?searchCriteria

Swagger contains more details. Please explore http://devdocs.magento.com/swagger/

answered Oct 9, 2019 at 5:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.