Magento 2 REST API PUT Example in PHP

curlintegrationmagento2ordersrest

I'm new to the Magento 2 REST API (this is also my first post in this forum, hi all), but during the past day have been able to retrieve information using it to my backend system. GET -requests are easy enough, but I'm struggling with getting a correct PUT request together.

What I need to do next is to update the order status from pending to processing, but just cannot figure out how to create a proper array to encode. I've endlessly Googled this up to the point where I've all but given up. There are a million examples on how to retrieve information from Magento, but pretty much NOTHING on how to make updates.

Here's my code:

(authentication part omitted – that works great)

        $icrement_id = "000000003";
        $requestUrl='http://www.example.com/rest/V1/orders?searchCriteria[filter_groups][0][filters][0][field]=increment_id&searchCriteria[filter_groups][0][filters][0][value]='.$increment_id; 
            $ch = curl_init($requestUrl);
            // Set status to processing
        $data_json = [
          "items"=> [
              "state"=> "processing"
          ]
        ];
        $data_json = json_encode($data);
        print '<br>JSON data: '.$data_json;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $headers, 'Content-Length: ' . strlen($data_json)));
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response  = curl_exec($ch);
        curl_close($ch);
        // Execute the query
        $result = curl_exec($ch);
        // Decode the result
        $result=  json_decode($result);
        print_r($result);

Questions:

  1. How to formulate $data, so that it would be correct (items[0] -> status from pending to processing)
  2. Is my PUT request correct otherwise? (f.ex. can I use a search as the requestURL)

So just to be crystal clear, I'm not trying to create a new order, but change an existing order. Examples that would give a 'for dummies' example of a REST API PUT -request would be appreciated also. Thanks.

Best Answer

As per the documentation in Magento Swagger Api you should use http://www.example.com/rest/V1/orders API with POST request to update order details. Here is the curl request example:

$entity_id = 3;
$request_url='http://www.example.com/rest/V1/orders';
$data_json = [
    "entity"=> [
        "entity_id" => $entity_id,
        "state" => "processing",
        "status" => "Processing"
    ]
];
$data_string = json_encode($data_json);
$ch = curl_init($request_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
);
$response = curl_exec($ch);
$response =  json_decode($response);
print_r($response);
curl_close($ch);

In case you want to update the order details using increment_id, than you will have to create your own API which accepts the increment_id as input for order detail updates. One of the example for cutom API is demonstrated here

Related Topic