⬆️ ⬇️

Working with OZON (Merchants) API using PHP

We are working on a large online store. And now there is a need from UT (1C Trade Management) to manage orders for O Ozon.



The meaning is as follows: there is a PostgreSQL database, 1C works with this database, enters data about incoming orders, changes order statuses. And there is also a PHP script that lies on the server and runs a crown every 3 minutes. What should this script do?





There were no problems with getting a token. Using the file_get_contents () function, I achieved the desired.

')

//   /auth/token/merchants?applicationid=[ApplicationId] (ApplicationId ) // $sign -  SHA1-HMAC ,      ,     - path- URL //  json,  { "token": "9895DDA48379484ABC51A4B193CDAE04", "expiration": 600 } $sign = hash_hmac('sha1','/auth/token/merchants?applicationid=albion','[  ]'); $token = file_get_contents('https://api.ozon.ru/auth/token/merchants?applicationid=[ApplicationId]&sign='.$sign); $token = substr($token,10,32); 


Then there were problems. Headers had to be sent along with the request. The API is documented rather badly, code examples are missing, I did not find a single example in Google. I had to work by trial and error. I first tried to act in the same way through file_get_contents () - to no avail. No matter what I did, an error was displayed that it was impossible to create a channel.



As a result, I used curl. The code for getting a list of new orders created (in json and in xml):



 //      json $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, 'https://api.ozon.ru/merchants/orders?StateName='); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, array( "x-ApplicationId: [ApplicationId]", "x-Token: ".$token, "x-ApiVersion: 0.1" )); $out = curl_exec($curl); curl_close($curl); //      xml $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, 'https://api.ozon.ru/merchants/orders?StateName='); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, array( "accept: application/xml", "content-type: application/xml", "x-ApplicationId: [ApplicationId]", "x-Token: ".$token, "x-ApiVersion: 0.1" )); $out1 = curl_exec($curl); curl_close($curl); 


The moments related to the creation of the xml file and changes in the postgreSQL database will not be described here, because this will be a digression from the topic.



Next, it was necessary to change the status of the order. To do this, among other things, in the body of a PUT request, it was necessary to transfer a new status (as it turned out, through long surveys, in the form of XML).



Changing the status of orders is as follows:



 //       foreach($ids as $item) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, 'https://api.ozon.ru/merchants/orders/state/'.$item); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POSTFIELDS, "<OrderStateRequest><StateSysName>ClientOrderStateMerchantAccepted</StateSysName></OrderStateRequest>"); curl_setopt($curl, CURLOPT_HTTPHEADER, array( "accept: application/xml", "content-type: application/xml", "x-ApplicationId: [ApplicationId]", "x-Token: ".$token, "x-ApiVersion: 0.1" )); $out = curl_exec($curl); curl_close($curl); } 


For different statuses you need to use different XML data. I will give a list of all available statuses:



 <OrderStatesResponse> <OrderStates> <OrderState> <Name></Name> <SysName>ClientOrderStateMerchantCreated</SysName> </OrderState> <OrderState> <Name> </Name> <SysName>ClientOrderStateMerchantAwaitingPayment</SysName> </OrderState> <OrderState> <Name></Name> <SysName>ClientOrderStateMerchantPaymentDone</SysName> </OrderState> <OrderState> <Name> </Name> <SysName>ClientOrderStateMerchantAccepted</SysName> </OrderState> <OrderState> <Name></Name> <SysName>ClientOrderStateMerchantSent</SysName> </OrderState> <OrderState> <Name></Name> <SysName>ClientOrderStateMerchantDone</SysName> </OrderState> <OrderState> <Name></Name> <SysName>ClientOrderStateMerchantCanceled</SysName> </OrderState> </OrderStates> </OrderStatesResponse> 


Also, sometimes there is a need to cancel the order. To do this, you must specify the reason for cancellation (also in the form of XML in the request body).



I will give an example (the specified reason: "The number of orders is more than is available"):



  //     $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, 'https://api.ozon.ru/merchants/orders/state/'.$order_number); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POSTFIELDS, "<OrderStateRequest><StateSysName>ClientOrderStateMerchantCanceled</StateSysName><CancelReason>OrderCountMoreThanRest</CancelReason></OrderStateRequest>"); curl_setopt($curl, CURLOPT_HTTPHEADER, array( "accept: application/xml", "content-type: application/xml", "x-ApplicationId: [ApplicationId]", "x-Token: ".$token, "x-ApiVersion: 0.1" )); $out = curl_exec($curl); curl_close($curl); 


In this way, you can manage Ozone orders from your site.



I hope my article will be useful to someone. Thank you all for your attention!



Some documentation

Source: https://habr.com/ru/post/303938/



All Articles