我正在尝试使用 PHP 的本机 SOAP 库实现的另一个端点来复制传统的 NuSOAP 端点。我通过在每个端点中返回相同的虚拟数组并比较每个端点返回的 xml 进行测试。
以下是旧服务器给出的示例响应
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://localhost/webservices-soap/servicios">
<SOAP-ENV:Body>
<ns1:LeerAtributosResponse xmlns:ns1="http://localhost/webservices-soap/servicios">
<return xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:ParamValue[2]">.
<item xsi:type="tns:ParamValue">
<parametro xsi:type="xsd:string">hola</parametro>
<valor xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="xsd:string[3]">
<item xsi:type="xsd:string">5</item>
<item xsi:type="xsd:string">6</item>
<item xsi:type="xsd:string">7</item>
</valor>
</item>
<item xsi:type="tns:ParamValue">
<parametro xsi:type="xsd:string">chau</parametro>
<valor xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="xsd:string[0]"/>
</item>
</return>
</ns1:LeerAtributosResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Run Code Online (Sandbox Code Playgroud)
这是新实现给出的响应:
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://localhost:8000/soap/servicios" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Body>
<ns1:LeerAtributosResponse>
<return SOAP-ENC:arrayType="ns1:ParamValue[2]" xsi:type="ns1:ArrayOfParamValue">
<item xsi:type="ns1:ParamValue">
<parametro xsi:type="xsd:string">hola</parametro>
<valor SOAP-ENC:arrayType="xsd:string[3]" xsi:type="ns1:ArrayOfString">
<item xsi:type="xsd:string">5</item>
<item xsi:type="xsd:string">6</item>
<item xsi:type="xsd:string">7</item>
</valor>
</item>
<item xsi:type="ns1:ParamValue">
<parametro xsi:type="xsd:string">chau</parametro>
<valor xsi:type="ns1:ArrayOfString"/>
</item>
</return>
</ns1:LeerAtributosResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Run Code Online (Sandbox Code Playgroud)
我发现回复之间存在以下差异:
这两个标签(“ns1”和“tns”)有什么区别?xml 中的这种差异是否会导致响应不同?
您在这里看到的是“XML 命名空间”。简而言之:
http://schemas.xmlsoap.org/soap/envelope/。xmlns:someprefix="http://example.com",并在元素和属性名称中使用,比如<someprefix:foo someprefix:bar="hello" />xmlns="http://example.com",它定义了没有前缀的元素所在的命名空间。http://localhost/webservices-soap/servicios或http://localhost:8000/soap/servicios。命名空间实际上应该是硬编码的,以便无论在哪里部署服务它都是相同的,但这对于测试来说并不重要。SOAP-ENVorsoap或envelopefor http://schemas.xmlsoap.org/soap/envelope/,但这些根本不会改变含义。在本例中,前缀tns和ns1正是不同 SOAP 库选择使用的前缀。举个简单的例子,这些 XML 片段具有完全相同的含义:
<?xml version="1.0"?>
<abc:example xmlns:abc="http://example.com">
<abc:foo>42</abc:foo>
</abc:example>
Run Code Online (Sandbox Code Playgroud)
和:
<?xml version="1.0"?>
<xyz:example xmlns:xyz="http://example.com">
<xyz:foo>42</xyz:foo>
</xyz:example>
Run Code Online (Sandbox Code Playgroud)
乃至:
<?xml version="1.0"?>
<abc:example xmlns:abc="http://example.com">
<xyz:foo xmlns:xyz="http://example.com">42</xyz:foo>
</abc:example>
Run Code Online (Sandbox Code Playgroud)
但这意味着不同的东西(记住前缀并不重要,只有 URI):
<?xml version="1.0"?>
<abc:example xmlns:abc="http://something.different.example.com">
<abc:foo>42</abc:foo>
</abc:example>
Run Code Online (Sandbox Code Playgroud)