PHP SoapClient 生成不同格式的 SOAP 请求

标签 php web-services soap

所以我尝试连接到第三方服务,但在 PHP 中遇到了一些问题。当我在 WebService Studio 中尝试服务请求时,它工作正常并且发送的请求如下所示:

<?xml version="1.0" encoding="utf-16"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <createUser xmlns="http://ws.example.com">
            <arg0 xmlns="">test@test.com</arg0>
            <arg1 xmlns="">123</arg1>
            <arg2 xmlns="">1234</arg2>
            <arg3 xmlns="">1234567890abcdef</arg3>
            <arg4 xmlns="">test</arg4>
            <arg5 xmlns="">user</arg5>
            <arg6 xmlns="">02472</arg6>
            <arg7 xmlns="">test@test.com</arg7>
            <arg8 xmlns="">A</arg8>
            <arg9 xmlns="">0</arg9>
            <arg10 xmlns="">true</arg10>
        </createUser>
    </soap:Body>
</soap:Envelope>

现在,当我尝试使用以下命令从 PHP 调用服务时:

$this->web_service->createAccount('test@test.com', 123, 1234, '1234567890abcdef', 'test', 'user', '12345', 'test@test.com', 'A', 0, true)

并调试请求,我得到了这个:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://ws.example.com">
    <SOAP-ENV:Body>
        <ns1:createUser/>
        <param1>123</param1>
        <param2>1234</param2>
        <param3>1234567890abdcef</param3>
        <param4>test</param4>
        <param5>user</param5>
        <param6>12345</param6>
        <param7>test@test.com</param7>
        <param8>A</param8>
        <param9>0</param9>
        <param10>true</param10>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

SoapClient 在 PHP 中生成的请求让我立刻想到了一些事情。第一件事是第一个参数(我第一次通过 test@test.com)没有在 param1 中传递,第二个参数是。接下来是对 createUser 的请求是一个自关闭标记,不包括传递的参数。那么显然整个结构与所使用的标签略有不同。

我尝试过使用数组(甚至没有达到抛出请求的程度)、将参数包装在 SoapParam 中、使用 __call() 和使用 __soapCall() 但这些都不能解决这个问题。

任何人都知道如何解决此问题,以便 PHP 中的 SoapClient 生成的请求与 WebService Studio 生成的请求匹配,而不是手动生成 soap 请求?

最佳答案

我遇到了类似的问题(使用自动关闭操作标签)

事实证明,问题出在我传递参数的方式上。预期参数的wsdl定义如下:

<s:element name="ValidateStudent">
<s:complexType>
<s:sequence>
    <s:element minOccurs="0" maxOccurs="1" name="studentNumber" type="s:string" />
    <s:element minOccurs="0" maxOccurs="1" name="surname" type="s:string" />
    <s:element minOccurs="0" maxOccurs="1" name="dob" type="s:string" />
    <s:element minOccurs="0" maxOccurs="1" name="clientIP" type="s:string" />
    <s:element minOccurs="0" maxOccurs="1" name="clientUserAgent" type="s:string" />
    <s:element minOccurs="0" maxOccurs="1" name="clientReferrer" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>



<wsdl:message name="ValidateStudentSoapIn">
  <wsdl:part name="parameters" element="tns:ValidateStudent" />
</wsdl:message>



<wsdl:operation name="ValidateStudent">
  <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Validation of user credentials to student portal</wsdl:documentation>
  <wsdl:input message="tns:ValidateStudentSoapIn" />
  <wsdl:output message="tns:ValidateStudentSoapOut" />
</wsdl:operation>



<wsdl:operation name="ValidateStudent">
  <soap:operation soapAction="http://test.example.com/ValidateStudent" style="document" />
  <wsdl:input>
    <soap:body use="literal" />
  </wsdl:input>
  <wsdl:output>
    <soap:body use="literal" />
  </wsdl:output>
</wsdl:operation>

因此,方法 ValidateStudent() 需要一个参数(也称为 ValidateStudent——这在第二部分中定义),它是第一部分中定义的复杂类型。

在我的例子中,我必须按如下方式传递参数(作为单个元素,键控为“ValidateStudent”,子元素的名称在 wsdl 中定义):

$soapParams = array('ValidateStudent' => array(
    'studentNumber'     => $stuCode,
    'surname'           => $lastName,
    'dob'               => $dob,
    'clientIP'          => $ip,
    'clientUserAgent'   => $uAgent,
    'clientReferrer'    => $referer
));

$response = $soapClient->__soapCall('ValidateStudent', $soapParams);

因此,基本上,请确保您理解正在使用的 wsdl 中列出的定义,并遵循其结构直至 T。

关于PHP SoapClient 生成不同格式的 SOAP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5093504/

相关文章:

java - 如何让 Axis2 处理数组?

php - Laravel 5.8 并不总是更新新编写的代码

PHP合并数组如果为空

c# - asp.net asmx Web 服务返回 xml 而不是 json

Java - Spring Ws - 在 XSD 文件中加载相对包含 (Tomcat 8)

c# - 我正在尝试使用 C# 中的 PHP SOAP 服务并在 VS 2010 中创建类包装器

php - Ajax删除功能不起作用

php - 无法连接实时服务器上的数据库

java - 将 servlet 移植到 Web 服务 - 访问上下文?

java - 使用 Web 服务客户端 JAXB 生成的类作为 JPA 实体