8

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

Anyone have idea about this?

2
  • You want to get order, customer info using REST API? Jan 11, 2017 at 10:02
  • Yes, I want order,customer info using REST API @KhoaTruongDinh
    – BornCoder
    Jan 11, 2017 at 10:03

5 Answers 5

18

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

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?
    – BornCoder
    Jan 11, 2017 at 11:26
  • Password encryption is one way. So you can't decrypt.
    – Sohel Rana
    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."}
    – BornCoder
    Jan 11, 2017 at 11:34
  • How can I create my magento api to use this code snippet ?
    – Ajwad Syed
    Apr 7, 2019 at 15:20
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
}
3

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

3
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.

1
  • 2
    not work it return all result not only 5 records
    – HaFiz Umer
    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/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.