Web 服务 - PHP SOAP 请求 navision Webservice


web services - PHP SOAP request to navision webservice

几天来,我一直在努力在PHP网上商店和具有NTLM身份验证的navision服务器之间建立连接。现在连接和身份验证已完成,我在执行正确请求时遇到问题。

对于此示例,我尝试使用函数(ItemPriceDiscInventory)来检索价格信息。

我从客户那里得到的网址是:http://IP:PORT/MSIteminfoTest/WS/Saniceve%20BV/Codeunit/ItemPriceDiscInventory

我尝试了 2 种方法来提出请求:

带有原始 XML 的卷曲帖子

原始 XML 由客户提供。这与测试程序(SOACleaner)在输入(本地)WSDL文件时给我的XML相同。

$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>';

我的要求:

$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);

这将返回 WSDL 文件,无论我包含什么 POST 值。如果我包含标头'SOAPAction: "ItemPriceDiscInventory"'它会给我错误:

<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> 

WSDL 如下所示:

<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 Soap客户端请求

好吧,我想,试试PHP的SOAP功能。我使用了NTLMSoapClient类进行身份验证,这似乎没问题。如果我使用上面给出的 URL 在 WSDL 模式下构造客户端,它无法解析 WSDL:

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

但是如果我在非 WSDL 模式下执行此操作,并在参数中给出位置,我就会得到一些结果......

    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');

这会导致错误。如果我收到最后一个请求和响应,它会给我:

<?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>

响应:

<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>

因此,他似乎正在尝试解析它,但请求格式不正确。注意:我不知道uri参数必须是什么。这是我可以从 WSDL 中读到的吗?

如果我看一下客户给我的请求和 SOAP 请求,它有很大的不同(第一个看起来根本不像 SOAP),但是对于 SOAP 请求,我似乎更接近了一点。

对此有什么想法吗?

我终于想通了。首先,我认为我的请求中的命名空间有问题。

我将 WSDL 文件保存在我们自己的服务器上,并将其用于 WSDL 模式下的 soapclient,以 navision 服务器为位置。这奏效了!

我不知道为什么我不能在 WSDL 模式下使用直接 URL,但这是一个可以接受的解决方法。