0

all! I'm implementing my custom API and want to send 2 parameters for my method. Here is my code:

  1. app/code/Openc/GCApi/etc/module.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
         <module name="Openc_GCApi" setup_version="1.0.0" />
    </config>
    
  2. app/code/Openc/GCApi/etc/webapi.xml

    <?xml version="1.0"?>
    <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd">
    <route url="/V1/opencGCApiGcapiapiStock/update/:productId/:data" method="POST">
    <service class="Openc\GCApi\Api\GcapiapiStockInterface" method="update"/>
    <resources>
        <resource ref="anonymous"/>
    </resources>
    

  3. app/code/Openc/GCApi/Api/GcapiapiStockInterface.php

    <?php
    namespace Openc\GCApi\Api;
    interface GcapiapiStockInterface
    {
        /**
         * Returns greeting message to user
         *
         * @param string $productId
         * @param string[] $data
         * @return bool
         */
         public function update($productId = NULL, $data);
     }
    
  4. app/code/Openc/GCApi/Model/GcapiapiStock.php

     <?php
     namespace Openc\GCApi\Model;
     use Openc\GCApi\Api\GcapiapiStockInterface;
     class GcapiapiStock implements GcapiapiStockInterface
     {
          /**
           * Returns greeting message to user
           *
           * @param string $productId
           * @param string[] $data
           * @return bool
           */
           public function update($productId = NULL, $data)
           {
               $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
               $logger = new \Zend\Log\Logger();
               $logger->addWriter($writer);
               $logger->info(print_r($productId, true));
               $logger->info(print_r($data, true));
               return false;
           }
      }
    
  5. app/code/Openc/GCApi/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="Openc\GCApi\Api\GcapiapiStockInterface" type="Openc\GCApi\Model\GcapiapiStock"/>
     </config>
    
  6. app/code/Openc/GCApi/registration.php

    <?php
    \Magento\Framework\Component\ComponentRegistrar::register(
          \Magento\Framework\Component\ComponentRegistrar::MODULE,
          'Openc_GCApi',
           __DIR__
    );
    

But in my custom log file I'm getting empty array as second parameter:

2017-11-06T16:12:26+00:00 INFO (6): 1
2017-11-06T16:12:26+00:00 INFO (6): Array
 (
 )

Fragment of my test script:

$result = $request->opencGCApiGcapiapiStockV1Update(array('productId' => 1, 
'data' => array('qty' => 26, 'is_in_stock' => 1, 'manage_stock' =>1, 'use_config_manage_stock' => 1, 'use_config_backorders' => 1, 'backorders' => 1)));

Could you please tell what can be the problem? Thanks,

0

The issue looks like it's with how/what params are being passed to the API call.

(i.e. It seems like you are passing 1 Array as a param, instead of 2 separate params 1 of which is a string and the other being an array)

Change your API call from

$result = $request->opencGCApiGcapiapiStockV1Update(array(  'productId' => 1, 
                                                            'data' => array('qty' => 26, 'is_in_stock' => 1, 'manage_stock' =>1, 'use_config_manage_stock' => 1,     'use_config_backorders' => 1, 'backorders' => 1)
                                                        )
                                                    );

to

$result = $request->opencGCApiGcapiapiStockV1Update(
                            'productId' => 1, 
                            'data' => array('qty' => 26, 'is_in_stock' => 1, 'manage_stock' =>1, 'use_config_manage_stock' => 1, 'use_config_backorders' => 1, 'backorders' => 1)
                        );

Good Luck! :)

  • Sorry, you've made mistake. I've tried this call $result = $request->opencGCApiGcapiapiStockV1Update(1, array('qty' => 26, 'is_in_stock' => 1, 'manage_stock' =>1, 'use_config_manage_stock' => 1, 'use_config_backorders' => 1, 'backorders' => 1)); but it gives error: Uncaught SoapFault exception: [SOAP-ENV:Client] Error cannot find parameter in .../apitest.php – Helmsmantest Nov 7 '17 at 8:03
  • Could you upload your (zipped) entire module on here, as I'd like to debug it on my localhost. – Sharath Kumar Nov 7 '17 at 16:46
  • Sure, here is the repo github.com/helmsmantest/Openc_GCApi. Thanks for your help! – Helmsmantest Nov 8 '17 at 8:03
  • Guys, any updates? – Helmsmantest Nov 9 '17 at 14:36
  • Sorry about the delay ... I don't see your test/client in the git repo. Could you upload that as well and include info on how you're testing/triggering it? – Sharath Kumar Nov 16 '17 at 18:59

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.