I hace created a custom api endpoint to receive data from a third party service. These are my modules files
etc/webapi.xml
<?xml version="1.0" ?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/matrid-nextapi/orders" method="POST">
<service class="Matrid\NextApi\Api\OrdersManagementInterface" method="postOrders"/>
<resources>
<resource ref='anonymous'/>
</resources>
</route>
</routes>
etc/di.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Matrid\NextApi\Api\OrdersManagementInterface" type="Matrid\NextApi\Model\OrdersManagement"/>
</config>
Api/OrdersManagementInterface.php <?php /** * Copyright © All rights reserved. * See COPYING.txt for license details. */ namespace Matrid\NextApi\Api;
interface OrdersManagementInterface
{
/**
*
* @param mixed $Orders
* @return mixed[]
*/
public function postOrders($Orders);
}
This is Model file for interface.
Model/OrdersManagement.php
<?php
/**
* Copyright © All rights reserved.
* See COPYING.txt for license details.
*/
namespace Matrid\NextApi\Model;
use Matrid\NextApi\Model\OrdersFactory;
class OrdersManagement implements \Matrid\NextApi\Api\OrdersManagementInterface
{
/**
*
* @param mixed $Orders
* @return mixed[]
*/
public function postOrders($Orders)
{
$response = $Orders;
$returnArray = json_encode($response);
return $returnArray;
}
}
Now I have created a custom file to test the API.
custom-test.php
<?php
$data = array (
'Orders' =>
array (
0 =>
array (
'ID' => 19558599,
'Brand' => 'Brand',
'Destination' => 'NextRDC',
'DateTimeStamp' => '2021-01-05T20:37:29+00:00',
'Currency' => 'GBP',
'Items' =>
array (
0 =>
array (
'ItemID' => 12345678,
'EAN' => '1234567890123',
'BrandSKU' => '12345678',
'Quantity' => 1,
'PromiseDate' => '2021-01-09',
'Price' => 67,
),
),
),
),
)
;
$curl = curl_init("https://staging.nonnon.co.uk/index.php/rest/V1/matrid-nextapi/orders");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$result = curl_exec($curl);
$data = json_decode($result, true);
var_dump($data);
?>
The output that I am receiving is this
[{"ID":19558599,"Brand":"Brand","Destination":"NextRDC","DateTimeStamp":"2021-01-05T20:37:29+00:00","Currency":"GBP","Items":[{"ItemID":12345678,"EAN":"1234567890123","BrandSKU":"12345678","Quantity":1,"PromiseDate":"2021-01-09","Price":67}]}]
If you check the output,the $data variable that is passed as post data in custom-test.php has lost its "Orders" key. And I know this has happened because of passing $Orders as comment in OrdersManagement.php file.
Now my question is what shall I do in order to prserve the Orders key. What should be the proper way to do this. Whatever json data is passed I shall be able to receive and print the entire data. Could someone plz guide me a way to do this.