如何在PHP中将对象参数传递给SOAP方法调用?

时间:2011-08-26 18:02:54

标签: php soap

我正在尝试发送一个soap请求,而我正在调用的方法需要定义任意数量的对象:

<xs:complexType name="thing">
  <xs:sequence>
    <xs:element minOccurs="0" name="string1" type="xs:string"/>
    <xs:element minOccurs="0" name="string2" type="xs:string"/>
    <xs:element minOccurs="0" name="string3" type="xs:long"/>
  </xs:sequence>
</xs:complexType>

如何在PHP中创建这样的对象并将其传递给soap方法?现在我的代码(不工作)看起来像这样:

$obj->string1 = 'something';
$obj->string2 = 'something';
$obj->string3 = 'something';
$param = new SoapParam(new SoapVar($obj, SOAP_ENC_OBJECT, 'method', 'http://ns'), 'paramName');

$soapClient->method($param);

更新

这是所示方法的请求的主体:

<SOAP-ENV:Body>
  <ns2:method xsi:type="ns1:method">
    <string1>something</string1>
    <string2>something</string2>
    <string3>something</string3>
  </ns2:placeHolds>
</SOAP-ENV:Body>

如果我只是传递$ obj而不是创建SoapParam,那么这就是请求中的主体:

<SOAP-ENV:Body>
  <ns1:method/>
</SOAP-ENV:Body>

1 个答案:

答案 0 :(得分:1)

$client = new SoapClient('endpoint');

$sequence->string1 = 'something';
$sequence->string2 = 'something';
$sequence->string3 = 'something';

$obj = array();
$obj['sequence'] = $sequence;

$param = new SoapParam(new SoapVar($obj, SOAP_ENC_OBJECT), 'paramName');

$client->__soapCall('method',array($param));