3

I have a function in an XML document and I want to return its parameters. My knowledge of connecting the two services is very limited. Do I need to create some middleware or is it possible just to call from Magento Swagger? Any help or resources to point me in the right direction would be super!

Function name: Get_Item_Quantity
This function takes Type, Code fields and returns Qty1, Qty2, Qty3, and Qty4
Parameters:

Type = 0 – input value
Code = ‘product100317’ – input value
Qty 1 – return value
Qty 2 – return value
Qty 3 – return value
Qty 4 – return value

How do I go about this?
This XML file does not appear to have any style information associated with it. The document tree is shown below.

<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="urn:microsoft-dynamics-schemas/codeunit/myWSAPI"
             targetNamespace="urn:microsoft-dynamics-schemas/codeunit/myWSAPI">
    <types>
        <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="urn:microsoft-dynamics-nav/xmlports/x00000"
                elementFormDefault="qualified" targetNamespace="urn:microsoft-dynamics-nav/xmlports/x00000">
            <complexType name="APIDataCapture">
                <sequence>
                    <element minOccurs="1" maxOccurs="1" name="Type" type="string"/>
                    <element minOccurs="1" maxOccurs="1" name="Code" type="string"/>
                    <element minOccurs="1" maxOccurs="1" name="Qty1" type="string"/>
                    <element minOccurs="1" maxOccurs="1" name="Qty2" type="string"/>
                    <element minOccurs="1" maxOccurs="1" name="Qty3" type="string"/>
                    <element minOccurs="1" maxOccurs="1" name="Qty4" type="string"/>
                </sequence>
            </complexType>
            <complexType name="Root" mixed="true">
                <sequence>
                    <element minOccurs="1" maxOccurs="unbounded" name="APIDataCapture" type="tns:APIDataCapture"/>
                </sequence>
            </complexType>
            <element name="Root" type="tns:Root"/>
        </schema>
        <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="urn:microsoft-dynamics-nav/xmlports/x50014"
                elementFormDefault="qualified" targetNamespace="urn:microsoft-dynamics-nav/xmlports/x50014">
            <complexType name="APIDataCapture">
                <sequence>
                    <element minOccurs="1" maxOccurs="1" name="Type" type="string"/>
                    <element minOccurs="1" maxOccurs="1" default="0" name="EntryNo" type="int"/>
                    <element minOccurs="1" maxOccurs="1" name="SalesOrderFound" type="string"/>
                    <element minOccurs="1" maxOccurs="1" name="SalesOrderNo" type="string"/>
                    <element minOccurs="1" maxOccurs="1" default="0" name="SalesOrderLineNo" type="int"/>
                    <element minOccurs="1" maxOccurs="1" name="SalesOrderETADate" type="string"/>
                    <element minOccurs="1" maxOccurs="1" name="SalesOrderItemNo" type="string"/>
                </sequence>
            </complexType>
            <complexType name="Root" mixed="true">
                <sequence>
                    <element minOccurs="1" maxOccurs="unbounded" name="APIDataCapture" type="tns:APIDataCapture"/>
                </sequence>
            </complexType>
            <element name="Root" type="tns:Root"/>
        </schema>
        <schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
                targetNamespace="urn:microsoft-dynamics-schemas/codeunit/MyWSAPI">
            <element name="Get_Item_ETA_Date">
                <complexType>
                    <sequence>
                        <element minOccurs="1" maxOccurs="1" name="itemNo" type="string"/>
                    </sequence>
                </complexType>
            </element>
            <element name="Get_Item_ETA_Date_Result">
                <complexType>
                    <sequence>
                        <element minOccurs="1" maxOccurs="1" name="return_value" type="date"/>
                    </sequence>
                </complexType>
            </element>
            <element name="Find_Duplicate_Ext_Doc_No">
                <complexType>
                    <sequence>
                        <element minOccurs="1" maxOccurs="1" name="custNo" type="string"/>
                        <element minOccurs="1" maxOccurs="1" name="extDocNo" type="string"/>
                    </sequence>
                </complexType>
            </element>
            <element name="Find_Duplicate_Ext_Doc_No_Result">
                <complexType>
                    <sequence>
                        <element minOccurs="1" maxOccurs="1" name="return_value" type="boolean"/>
                    </sequence>
                </complexType>
            </element>
            <element name="Get_Item_Quantity">
                <complexType>
                    <sequence>
                        <element xmlns:q1="urn:microsoft-dynamics-nav/xmlports/x00000" minOccurs="1" maxOccurs="1"
                                 name="aPIDataCapture" type="q1:Root"/>
                    </sequence>
                </complexType>
            </element>
            <element name="Get_Item_Quantity_Result">
                <complexType>
                    <sequence>
                        <element xmlns:q2="urn:microsoft-dynamics-nav/xmlports/x00000" minOccurs="1" maxOccurs="1"
                                 name="aPIDataCapture" type="q2:Root"/>
                    </sequence>
                </complexType>
            </element>
            <element name="GetSalesOrderETA">
                <complexType>
                    <sequence>
                        <element xmlns:q3="urn:microsoft-dynamics-nav/xmlports/x50014" minOccurs="1" maxOccurs="1"
                                 name="aPIDataCapture" type="q3:Root"/>
                    </sequence>
                </complexType>
            </element>
            <element name="GetSalesOrderETA_Result">
                <complexType>
                    <sequence>
                        <element xmlns:q4="urn:microsoft-dynamics-nav/xmlports/x50014" minOccurs="1" maxOccurs="1"
                                 name="aPIDataCapture" type="q4:Root"/>
                    </sequence>
                </complexType>
            </element>
        </schema>
    </types>
    <message name="Get_Item_ETA_Date">
        <part name="parameters" element="tns:Get_Item_ETA_Date"/>
    </message>
    <message name="Get_Item_ETA_Date_Result">
        <part name="parameters" element="tns:Get_Item_ETA_Date_Result"/>
    </message>
    <message name="Find_Duplicate_Ext_Doc_No">
        <part name="parameters" element="tns:Find_Duplicate_Ext_Doc_No"/>
    </message>
    <message name="Find_Duplicate_Ext_Doc_No_Result">
        <part name="parameters" element="tns:Find_Duplicate_Ext_Doc_No_Result"/>
    </message>
    <message name="Get_Item_Quantity">
        <part name="parameters" element="tns:Get_Item_Quantity"/>
    </message>
    <message name="Get_Item_Quantity_Result">
        <part name="parameters" element="tns:Get_Item_Quantity_Result"/>
    </message>
    <message name="GetSalesOrderETA">
        <part name="parameters" element="tns:GetSalesOrderETA"/>
    </message>
    <message name="GetSalesOrderETA_Result">
        <part name="parameters" element="tns:GetSalesOrderETA_Result"/>
    </message>
    <portType name="MyWSAPI_Port">
        <operation name="Get_Item_ETA_Date">
            <input name="Get_Item_ETA_Date" message="tns:Get_Item_ETA_Date"/>
            <output name="Get_Item_ETA_Date_Result" message="tns:Get_Item_ETA_Date_Result"/>
        </operation>
        <operation name="Find_Duplicate_Ext_Doc_No">
            <input name="Find_Duplicate_Ext_Doc_No" message="tns:Find_Duplicate_Ext_Doc_No"/>
            <output name="Find_Duplicate_Ext_Doc_No_Result" message="tns:Find_Duplicate_Ext_Doc_No_Result"/>
        </operation>
        <operation name="Get_Item_Quantity">
            <input name="Get_Item_Quantity" message="tns:Get_Item_Quantity"/>
            <output name="Get_Item_Quantity_Result" message="tns:Get_Item_Quantity_Result"/>
        </operation>
        <operation name="GetSalesOrderETA">
            <input name="GetSalesOrderETA" message="tns:GetSalesOrderETA"/>
            <output name="GetSalesOrderETA_Result" message="tns:GetSalesOrderETA_Result"/>
        </operation>
    </portType>
    <binding name="MyWSAPI_Binding" type="tns:MyWSAPI_Port">
        <binding xmlns="http://schemas.xmlsoap.org/wsdl/soap/" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="Get_Item_ETA_Date">
            <operation xmlns="http://schemas.xmlsoap.org/wsdl/soap/"
                       soapAction="urn:microsoft-dynamics-schemas/codeunit/MyWSAPI:Get_Item_ETA_Date" style="document"/>
            <input name="Get_Item_ETA_Date">
                <body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="literal"/>
            </input>
            <output name="Get_Item_ETA_Date_Result">
                <body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="literal"/>
            </output>
        </operation>
        <operation name="Find_Duplicate_Ext_Doc_No">
            <operation xmlns="http://schemas.xmlsoap.org/wsdl/soap/"
                       soapAction="urn:microsoft-dynamics-schemas/codeunit/myWSAPI:Find_Duplicate_Ext_Doc_No"
                       style="document"/>
            <input name="Find_Duplicate_Ext_Doc_No">
                <body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="literal"/>
            </input>
            <output name="Find_Duplicate_Ext_Doc_No_Result">
                <body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="literal"/>
            </output>
        </operation>
        <operation name="Get_Item_Quantity">
            <operation xmlns="http://schemas.xmlsoap.org/wsdl/soap/"
                       soapAction="urn:microsoft-dynamics-schemas/codeunit/myWSAPI:Get_Item_Quantity" style="document"/>
            <input name="Get_Item_Quantity">
                <body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="literal"/>
            </input>
            <output name="Get_Item_Quantity_Result">
                <body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="literal"/>
            </output>
        </operation>
        <operation name="GetSalesOrderETA">
            <operation xmlns="http://schemas.xmlsoap.org/wsdl/soap/"
                       soapAction="urn:microsoft-dynamics-schemas/codeunit/myWSAPI:GetSalesOrderETA" style="document"/>
            <input name="GetSalesOrderETA">
                <body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="literal"/>
            </input>
            <output name="GetSalesOrderETA_Result">
                <body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="literal"/>
            </output>
        </operation>
    </binding>
    <service name="myWSAPI">
        <port name="myWSAPI_Port" binding="tns:MyWSAPI_Binding">
            <address xmlns="http://schemas.xmlsoap.org/wsdl/soap/"
                     location="http://test.myapi.local:7947/my/path/here/myWSAPI"/>
        </port>
    </service>
</definitions>
2
  • You're trying to call this SOAP API from a rest endpoint you're making through Magento? Or..?
    – sdaugherty
    Feb 19, 2021 at 19:55
  • yes, that is correct. How do i go about this?
    – Singleton
    Feb 22, 2021 at 15:59

1 Answer 1

2

I believe you already created a custom Rest API and the only missing piece of the puzzle is API call. Although I have added all steps to create a custom API as well.

This is how I am calling SOAP API using soap call.

  1. Create CustomInterface.php in path app/code/Test/CustomApi/Api/CustomInterface.php

    namespace Test\CustomApi\Api;
    
    interface CustomInterface
    {
        /**
         * GET for Qty api
         * @param int $type
         * @param string $code
         * @return string
         */
    
         public function getQty($type,$code);
    }
    
  2. Create Custom.php in path app/code/Test/CustomApi/Model/Api/Custom.php

    namespace Test\CustomApi\Model\Api;
    
    use Psr\Log\LoggerInterface;
    
    class Custom
    {
        protected $logger;
    
        public function __construct(
            LoggerInterface $logger
        )
        {
            $this->logger = $logger;
        }
    
        /**
         * @inheritdoc
         */
        public function getQty($type,$code) {
            $response = ['success' => false];
    
            try {
                // Logic to call soap service here
                $qty = $this->getItemQty($type,$code);
                $response = ['success' => true, 'qty' => $qty];
    
            } catch (\Exception $e) {
                $response = ['success' => false, 'message' => $e->getMessage()];
                $this->logger->info($e->getMessage());
            }
    
            $returnArray = json_encode($response);
            return $returnArray;
        }
    
    
        /**
         * Soap API call 
         *
         * @param int $type
         * @param string $code
         * @return void
         */
        public function getItemQty($type,$code)
        {
            $soapRequest = '<?xml version="1.0" encoding="utf-8"?>
            <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
              <soap12:Body>
                <Get_Item_Quantity xmlns="http://test.com/">
                    <Type>' . $type . '</Type>
                    <Code>' . $code . '</Code>
                </Get_Item_Quantity>
              </soap12:Body>
            </soap12:Envelope>';
            $header = array(
                "Content-type: text/xml;charset=\"utf-8\"",
                "Accept: text/xml",
            );
            $soapHandler = curl_init();
            curl_setopt($soapHandler, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
            curl_setopt($soapHandler, CURLOPT_URL, "http://test.com/");
            curl_setopt($soapHandler, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($soapHandler, CURLOPT_CONNECTTIMEOUT, 0);
            curl_setopt($soapHandler, CURLOPT_POST, true);
            curl_setopt($soapHandler, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($soapHandler, CURLOPT_POSTFIELDS, $soapRequest);
            curl_setopt($soapHandler, CURLOPT_HTTPHEADER, $header);
            curl_setopt($soapHandler, CURLOPT_FOLLOWLOCATION, false);
            //header('Content-type: text/xml');
            $result = curl_exec($soapHandler);
            $qtyData = array();
            if ($result === false) {
                $err = 'Curl error: ' . curl_error($soapHandler);
                curl_close($soapHandler);
                $this->logger->info($err);
            } else {
                curl_close($soapHandler);
                $xml = simplexml_load_string($result);
                $xml->registerXPathNamespace('soap', 'http://www.w3.org/2003/05/soap-envelope');
                $result = $xml->xpath('/soap:Envelope/soap:Body');
                $res = $result[0]->Get_Item_Quantity_Result;
                $qtyData = simplexml_load_string($res);
            }
            return $qtyData;
        }
    }
    

You should be able see this endpoint in swagger. http://URL/swagger

enter image description here

16
  • Thanks for your help on this and i'm testing it right now. I'm assuming that i plug my Soap URL into where you put test.com correct? @kamlesh.bar
    – Singleton
    Mar 17, 2021 at 16:08
  • Are you there? Put the link to my destination where my xmlSoap file resides where you put test.com? @kamlesh.bar
    – Singleton
    Mar 19, 2021 at 18:19
  • Sorry I didn't get your question, you need to change URL 1.<Get_Item_Quantity xmlns="test.com" > 2. curl_setopt($soapHandler, CURLOPT_URL, "test.com/"); here. Mar 22, 2021 at 1:58
  • Yes, do i change it to where my endpoint soap xml document resides like here : test.myapi.local:7947/my/path/here/myWSAPI?????
    – Singleton
    Mar 22, 2021 at 12:57
  • How did you get to that swagger page in particular where you type in the testCustomApiCustomV1? Currently i'm on my swagger page but i don't know how to enter the information.
    – Singleton
    Mar 23, 2021 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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