1

I'm implementing custom API for Magento 2. My client want the response to be as associative array. Here is my class method:

class Gcapiapi implements GcapiapiInterface
{
     /**
       * Returns greeting message to user
       *
       * @param string[] $products
       * @return array
       */

       public function list($products  = NULL)
       {
          $result = array();
          $stockItems = ...
          ...
          foreach($stockItems as $stockItem){
             $itemData = array('product_id' => $product->getId(), 'sku' => $productSku, 'qty' => $stockItem->getQty(), 'is_in_stock' => $stockItem->getIsInStock());
             $result[] = $itemData;
          }
          return $result;
       }
}

Here is interface declaration:

interface GcapiapiInterface
{
   /**
   * Returns greeting message to user
   *
   * @param string[] $products
   * @return array
   */
    public function list($products = NULL);

}

I've added logs and I see that my method list executing. But in response I'm getting 500 error. In exception.log I see the error:

Message: Class "array" does not exist. Please note that namespace must be specified.

I want to get the following response:

array (size=2)
0 => 
array (size=4)
  'product_id' => string '3708' (length=4)
  'sku' => string 'W3L2221LDCB2' (length=12)
  'qty' => string '228.0000' (length=8)
  'is_in_stock' => string '1' (length=1)
1 => 
array (size=4)
  'product_id' => string '3709' (length=4)
  'sku' => string 'W7L1226E5C96' (length=12)
  'qty' => string '23.0000' (length=7)
  'is_in_stock' => string '1' (length=1)

Can I get SOAP response as associative array somehow? Thanks,

0

Rewrite your class as:

class Gcapiapi implements GcapiapiInterface
{
     /**
       * Returns greeting message to user
       *
       * @param string[] $products
       * @return mixed[]
       */

       public function list($products  = NULL)
       {
          $result = array();
          $stockItems = ...
          ...
          foreach($stockItems as $stockItem){
             $itemData = array('product_id' => $product->getId(), 'sku' => $productSku, 'qty' => $stockItem->getQty(), 'is_in_stock' => $stockItem->getIsInStock());
             $result[] = $itemData;
          }
          return $result;
       }
}

You can see the change in return type: array -> mixed[]
It's strongly advised to use Data interface in such cases even though mixed[] will work for you.

4
  • In this case.Instead of associative array i'm getting std object:stdClass Object ( [result] => stdClass Object ( [item] => Array ( [0] => stdClass Object ( [item] => Array ( [0] => stdClass Object ( [key] => product_id [value] => 1 ) ... – Helmsmantest Nov 6 '17 at 15:12
  • then cast with (array) $result after you get the result. – MagePsycho Nov 6 '17 at 15:13
  • Unfortunatelly we can not modify code on the client's side. Is there some other method to get associative array as the result of soap function calling? – Helmsmantest Nov 6 '17 at 16:01
  • Any updates, guys? – Helmsmantest Nov 9 '17 at 14:39
-1

Once you've received the response from Magento, you can use the following snippet to convert the SOAP object into a PHP associative array:

<?php

$response = simplexml_load_string($soapResponse->any);
$response = json_decode(json_encode($response), true);

Assuming that $soapResponse->any has this content:

<data count="1" count_available="1">
    <row id="1">
        <sku>1830DMG</sku>
        <price>74.06</price>
        <stockid>519745</stockid>
    </row>
</data>

You'll get an array which looks like this:

$ php -f apitest3.php
array(2) {
    ["@attributes"]=>
  array(2) {
        ["count"]=>
    string(1) "1"
        ["count_available"]=>
    string(1) "1"
  }
  ["row"]=>
  array(4) {
        ["@attributes"]=>
    array(1) {
            ["id"]=>
      string(1) "1"
    }
    ["sku"]=>
    string(7) "1830DMG"
        ["price"]=>
    string(5) "74.06"
        ["stockid"]=>
    string(6) "519745"
  }
}

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.