PHP SOAP request to navision webservice

4.9k views Asked by At

For days I've been struggling to get a connection between a PHP webshop and a navision server with NTLM authentication. Now that the connection and authentication is done, I'm having problems with doing a correct request.

For this example I try to use a function (ItemPriceDiscInventory) to retrieve price information.

The url I have from the customer is: http://IP:PORT/MSIteminfoTest/WS/Saniceve%20BV/Codeunit/ItemPriceDiscInventory

I have tried 2 methods to make the request:

curl post with raw XML

The raw XML is given by the customer. This is the same XML the test program (SOACleaner) gives me when entered the (local) WSDL file.

$request = '
<InputItemPriceDiscInventory_PortGetPriceDiscInvInformation xmlns="http://xyrow.com">
<Body xmlns="">
<GetPriceDiscInvInformation xmlns="urn:microsoft-dynamics-schemas/codeunit/ItemPriceDiscInventory">
  <itemPriceDiscInventory>
    <Item xmlns="urn:microsoft-dynamics-nav/xmlports/x50005">
      <Klantnummer />
      <Artikelnr />
      <Aantal />
      <KortingsPercentage1 />
      <KortingsPercentage2 />
      <NettoPerEenheid />
      <BeschikbareVoorraad />
      <WebShopVoorraad />
    </Item>
  </itemPriceDiscInventory>
  <customerNo>1234</customerNo>
  <itemNo>5678</itemNo>
  <quantityVarDec>1</quantityVarDec>
</GetPriceDiscInvInformation>
</Body>
</InputItemPriceDiscInventory_PortGetPriceDiscInvInformation>';

My request:

$ch = curl_init(); 
$headers = array( 
      'Method: POST', 
        'Connection: Keep-Alive',       
        'Content-Type: text/xml; charset=utf-8',             
        "Host: {$ip}:{$port}",
        "Accept: */*"                       
    );      
curl_setopt($ch, CURLOPT_URL,            $url );        
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_POST,           true );        
curl_setopt($ch, CURLOPT_POSTFIELDS,    $request); 
curl_setopt($ch, CURLOPT_USERPWD, 'usr:pass');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM );
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);          
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);     
curl_setopt($ch, CURLOPT_FAILONERROR, 0);

This returns the WSDL file, no matter what POST values I include. If I include the header 'SOAPAction: "ItemPriceDiscInventory"' it gives me the error:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><s:Fault><faultcode xmlns:a="urn:microsoft-dynamics-schemas/error">a:System.Net.WebException</faultcode><faultstring xml:lang="nl-NL">Soap message is invalid!</faultstring><detail><string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Soap message is invalid!</string></detail></s:Fault></s:Body></s:Envelope> 

The WSDL looks like:

<definitions targetNamespace="urn:microsoft-dynamics-schemas/codeunit/ItemPriceDiscInventory" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="urn:microsoft-dynamics-schemas/codeunit/ItemPriceDiscInventory">
<types>
    <schema elementFormDefault="qualified" targetNamespace="urn:microsoft-dynamics-nav/xmlports/x50005" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="urn:microsoft-dynamics-nav/xmlports/x50005">
        <complexType name="Item">
            <sequence>
                <element minOccurs="1" maxOccurs="1" name="Klantnummer" type="string"/>
                <element minOccurs="1" maxOccurs="1" name="Artikelnr" type="string"/>
                <element minOccurs="1" maxOccurs="1" name="Aantal" type="string"/>
                <element minOccurs="1" maxOccurs="1" name="KortingsPercentage1" type="string"/>
                <element minOccurs="1" maxOccurs="1" name="KortingsPercentage2" type="string"/>
                <element minOccurs="1" maxOccurs="1" name="NettoPerEenheid" type="string"/>
                <element minOccurs="1" maxOccurs="1" name="BeschikbareVoorraad" type="string"/>
                <element minOccurs="1" maxOccurs="1" name="WebShopVoorraad" type="string"/>
            </sequence>
        </complexType>
        <complexType name="ItemPriceDiscInvInfo" mixed="true">
            <sequence>
                <element minOccurs="1" maxOccurs="1" name="Item" type="tns:Item"/>
            </sequence>
        </complexType>
        <element name="ItemPriceDiscInvInfo" type="tns:ItemPriceDiscInvInfo"/>
    </schema>
    <schema elementFormDefault="qualified" targetNamespace="urn:microsoft-dynamics-schemas/codeunit/ItemPriceDiscInventory" xmlns="http://www.w3.org/2001/XMLSchema">
        <element name="GetPriceDiscInvInformation">
            <complexType>
                <sequence>
                    <element minOccurs="1" maxOccurs="1" name="itemPriceDiscInventory" type="q1:ItemPriceDiscInvInfo" xmlns:q1="urn:microsoft-dynamics-nav/xmlports/x50005"/>
                    <element minOccurs="1" maxOccurs="1" name="customerNo" type="string"/>
                    <element minOccurs="1" maxOccurs="1" name="itemNo" type="string"/>
                    <element minOccurs="1" maxOccurs="1" name="quantityVarDec" type="decimal"/>
                </sequence>
            </complexType>
        </element>
        <element name="GetPriceDiscInvInformation_Result">
            <complexType>
                <sequence>
                    <element minOccurs="1" maxOccurs="1" name="itemPriceDiscInventory" type="q2:ItemPriceDiscInvInfo" xmlns:q2="urn:microsoft-dynamics-nav/xmlports/x50005"/>
                </sequence>
            </complexType>
        </element>
    </schema>
</types>
<message name="GetPriceDiscInvInformation">
    <part name="parameters" element="tns:GetPriceDiscInvInformation"/>
</message>
<message name="GetPriceDiscInvInformation_Result">
    <part name="parameters" element="tns:GetPriceDiscInvInformation_Result"/>
</message>
<portType name="ItemPriceDiscInventory_Port">
    <operation name="GetPriceDiscInvInformation">
        <input name="GetPriceDiscInvInformation" message="tns:GetPriceDiscInvInformation"/>
        <output name="GetPriceDiscInvInformation_Result" message="tns:GetPriceDiscInvInformation_Result"/>
    </operation>
</portType>
<binding name="ItemPriceDiscInventory_Binding" type="tns:ItemPriceDiscInventory_Port">
    <binding transport="http://schemas.xmlsoap.org/soap/http" xmlns="http://schemas.xmlsoap.org/wsdl/soap/"/>
    <operation name="GetPriceDiscInvInformation">
        <operation soapAction="urn:microsoft-dynamics-schemas/codeunit/ItemPriceDiscInventory:GetPriceDiscInvInformation" style="document" xmlns="http://schemas.xmlsoap.org/wsdl/soap/"/>
        <input name="GetPriceDiscInvInformation">
            <body use="literal" xmlns="http://schemas.xmlsoap.org/wsdl/soap/"/>
        </input>
        <output name="GetPriceDiscInvInformation_Result">
            <body use="literal" xmlns="http://schemas.xmlsoap.org/wsdl/soap/"/>
        </output>
    </operation>
</binding>
<service name="ItemPriceDiscInventory">
    <port name="ItemPriceDiscInventory_Port" binding="tns:ItemPriceDiscInventory_Binding">
        <address location="http://IP:PORT/MSIteminfoTest/WS/Saniceve%20BV/Codeunit/ItemPriceDiscInventory" xmlns="http://schemas.xmlsoap.org/wsdl/soap/"/>
    </port>
</service>
</definitions>

PHP SoapClient request

Ok, I thought, try the SOAP functionality of PHP. I used a NTLMSoapClient class for the authentication, which seems to be OK. If I construct the client in WSDL mode, with the URL given above, it can't parse the WSDL:

[WSDL] SOAP-ERROR: Parsing WSDL: Couldn't load from 'URL' : Start tag expected, '<' 

But if I do it in non WSDL mode, and give the location in the parameters, I'm having some result...

    stream_wrapper_unregister('http'); 
stream_wrapper_register('http', 'MyServiceProviderNTLMStream') or die("Failed to register protocol");

$client = new MyServiceNTLMSoapClient(null,array(   'location' => $url,
                                                    'uri'      => "http://schemas.xmlsoap.org/wsdl/soap/",
                                                    'trace'    => 1));  

$itemPriceDiscInventory = array(
                "Item" => array(
                    "Klantnummer" => null,
                    "Artikelnr" => null,
                    "Aantal" => null,
                    "KortingsPercentage" => null,
                    "NettoPerEenheid" => null,
                    "Voorraad" => null
                )
            );

$params = array(
            "itemPriceDiscInventory" => $itemPriceDiscInventory,
            "customerNo" => '0000265',
            "itemNo" => 'SAN483539',
            "quantityVarDec" => '1.0'
);


echo $client->GetPriceDiscInvInformation($params);  

stream_wrapper_restore('http');

This results in an error. If I get the last request and response it gives me:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
    <ns1:GetPriceDiscInvInformation>
        <param0 xsi:type="ns2:Map">
            <item>
                <key xsi:type="xsd:string">itemPriceDiscInventory</key>
                <value xsi:type="ns2:Map">
                    <item>
                        <key xsi:type="xsd:string">Item</key>
                        <value xsi:type="ns2:Map">
                            <item>
                                <key xsi:type="xsd:string">Klantnummer</key>
                                <value xsi:nil="true"/>
                            </item>
                            <item>
                                <key xsi:type="xsd:string">Artikelnr</key>
                                <value xsi:nil="true"/>
                            </item>
                            <item>
                                <key xsi:type="xsd:string">Aantal</key>
                                <value xsi:nil="true"/>
                            </item>
                            <item>
                                <key xsi:type="xsd:string">KortingsPercentage</key>
                                <value xsi:nil="true"/>
                            </item>
                            <item>
                                <key xsi:type="xsd:string">NettoPerEenheid</key>
                                <value xsi:nil="true"/>
                            </item>
                            <item>
                                <key xsi:type="xsd:string">Voorraad</key>
                                <value xsi:nil="true"/>
                            </item>
                        </value>
                    </item>
                </value>
            </item>
            <item>
                <key xsi:type="xsd:string">customerNo</key>
                <value xsi:type="xsd:string">0000265</value>
            </item>
            <item>
                <key xsi:type="xsd:string">itemNo</key>
                <value xsi:type="xsd:string">SAN483539</value>
            </item>
            <item>
                <key xsi:type="xsd:string">quantityVarDec</key>
                <value xsi:type="xsd:string">1.0</value>
            </item>
        </param0>
    </ns1:GetPriceDiscInvInformation>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Response:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
    <s:Fault>
        <faultcode xmlns:a="urn:microsoft-dynamics-schemas/error">a:Microsoft.Dynamics.Nav.Service.WebServices.ServiceBrokerException</faultcode>
        <faultstring xml:lang="nl-NL">Parameter itemPriceDiscInventory in method GetPriceDiscInvInformation in service ItemPriceDiscInventory is null!</faultstring>
        <detail>
            <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Parameter itemPriceDiscInventory in method GetPriceDiscInvInformation in service ItemPriceDiscInventory is null!</string>
        </detail>
    </s:Fault>
</s:Body>

So, it seems that he is trying to parse it, but the request is not in correct format. Note: I don't know exactly what the uri parameter has to be. Is this something I can read from the WSDL?

If I look at the request that the customer gave me and the SOAP request, it differs a lot (first one doesn't looks like SOAP at all), but with the SOAP request, it seems to be that I get a bit closer.

Any thoughts on this?

1

There are 1 answers

3
Frank Soeters On BEST ANSWER

I finally figured it out. First I thought that there was something wrong with the namespaces in my request.

I saved the WSDL file on our own server, and used it for the soapclient in WSDL mode, with the navision server as location. This worked!

I don't know why I can't use the direct URL in WSDL mode, but this is an acceptable workaround.