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

时间:2011-02-23 16:10:28

标签: 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的请求是一个自闭项标记,不包括传递的参数。然后显然整个结构与使用的标签略有不同。

我已经尝试过使用一个数组(它甚至不会抛出请求),使用__call()和使用__soapCall()将参数包装在SoapParam中,但是没有一个能解决这个问题。 / p>

任何人都知道什么可以解决这个问题,所以SoapClient在PHP中生成的请求与WebService Studio生成的请求匹配,而不是手动生成soap请求?

2 个答案:

答案 0 :(得分:6)

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

事实证明,问题在于我如何通过参数。来自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.

答案 1 :(得分:0)

我认为使用带有索引

参数名称的数组可能会更好
$this->webService->createAccount(array('arg0'=>'test@test.com')...);

或使用__soapCall功能

相关问题