从PHP SoapServer返回PHP数组

时间:2009-07-23 23:06:56

标签: php soap wsdl soapserver

我对“创建服务方面”的Soap相对较新,所以请提前预测我正在修改的任何术语。

是否可以从使用PHP的SoapServer类设置的远程过程肥皂服务返回PHP数组?

我有一个WSDL(盲目地遵循教程而构建),部分看起来像这样

<message name='genericString'>
    <part name='Result' type='xsd:string'/>
</message>

<message name='genericObject'>
    <part name='Result' type='xsd:object'/>
</message>

<portType name='FtaPortType'>       
    <operation name='query'>
        <input message='tns:genericString'/>
        <output message='tns:genericObject'/>
    </operation>        
</portType>

我正在调用的PHP方法是命名查询,看起来像这样

public function query($arg){
    $object = new stdClass();
    $object->testing = $arg;
    return $object;     
}

这允许我打电话

$client = new SoapClient("http://example.com/my.wsdl");
$result = $client->query('This is a test');

并且结果转储看起来像

object(stdClass)[2]
    public 'result' => string 'This is a test' (length=18)

我想从查询方法返回本机PHP数组/集合。如果我更改我的查询方法以返回数组

public function query($arg) {
    $object = array('test','again');
    return $object;
}

它被序列化为客户端的对象。

object(stdClass)[2]
    public 'item' => 
        array
            0 => string 'test' (length=4)
            1 => string 'again' (length=5)

这是有道理的,因为我在我的WSDL中将xsd:object特定为Result类型。如果可能的话,我想返回一个未包含在Object中的本机PHP数组。我的直觉说有一个特定的xsd:类型可以让我实现这个目标,但我不知道。我还决定将被序列化为ArrayObject的对象。

请勿在WSDL的技术细节中拒绝接受我的教育。我正在努力掌握基本概念

3 个答案:

答案 0 :(得分:5)

小技巧 - 编码为JSON对象,解码回递归关联数组:

$data = json_decode(json_encode($data), true);

答案 1 :(得分:3)

我使用this WSDL generator创建描述文件。

返回字符串数组是我的Web服务所做的事情,这是WSDL的一部分:

<wsdl:types>
<xsd:schema targetNamespace="http://schema.example.com">
  <xsd:complexType name="stringArray">
    <xsd:complexContent>
      <xsd:restriction base="SOAP-ENC:Array">
        <xsd:attribute ref="SOAP-ENC:arrayType" wsdl:arrayType="xsd:string[]" />
      </xsd:restriction>
    </xsd:complexContent>
  </xsd:complexType>
</xsd:schema>

</wsdl:types>
<message name="notifyRequest">
  <part name="parameters" type="xsd:string" />
</message>
<message name="notifyResponse">
  <part name="notifyReturn" type="tns:stringArray" />
</message>

然后定义API函数notify

<wsdl:operation name="notify">
  <wsdl:input message="tns:notifyRequest" />
  <wsdl:output message="tns:notifyResponse" />
</wsdl:operation>

答案 2 :(得分:0)

Alan,为什么不在客户收到回复时将您的对象转换为数组?

e.g。

(array) $object;

这会将你的stdClass对象转换为数组,没有可衡量的开销,在PHP中是O(1)。

您可能想尝试将类型从xsd:object更改为soap-enc:Array。

相关问题