1

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.

1
  • Could be an issue of wrong declaration of params and returns of the functions, could you please try to change the param and return from the postOrders to string instead of mixed? Sep 3 at 15:51
0

If you do not create ResultInterface for you API, magento will change the first key of result to 0 index.

There are two ways to handle this

First: This one is bit easy method, but not the magneto way, Use array to return your data from API model like this:

public function postOrders($Orders)
{
    $response = $Orders;
    $returnArray = json_encode($response);
    return [$returnArray]; 
}

This will send the data in [0] index and you will get all the info in the array in location [0]

Second: This is the actual way of magento API. You need create separate ResultInterface for you API. you can see cms page API for you sample response. in file vendor/magento/module-cms/Model/PageRepository.php check method getList while sending response it is using interface Magento\Cms\Api\Data\PageSearchResultsInterface to creating response.

You need to create this type of ResultInterface for you API to send data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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