3

在“创建服务端”方面,我对 Soap 比较陌生,因此请提前对我正在使用的任何术语表示歉意。

是否可以从使用 PHP 的 SoapServer 类设置的远程过程 Soap 服务返回 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)

这是有道理的,因为我xsd:object在我的 WSDL 中将 a 指定为 Result 类型。如果可能的话,我想返回一个未包含在 Object 中的原生 PHP 数组。我的直觉说有一个特定的 xsd:type 可以让我完成这个,但我不知道。我也会满足于将对象序列化为ArrayObject.

不要阻止我学习 WSDL 的技术细节。我正在尝试掌握以下基本概念

4

3 回答 3

7

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

$data = json_decode(json_encode($data), true);
于 2013-10-09T19:27:51.970 回答
3

我使用这个 WSDL 生成器来创建描述文件。

返回字符串数组是我的 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>
于 2009-07-24T02:00:25.767 回答
0

Alan,当您的客户收到响应时,为什么不将您的对象转换为数组?

例如

(array) $object;

这会将您的 stdClass 对象转换为数组,对此没有可测量的开销,并且在 PHP 中为 O(1)。

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

于 2009-07-23T23:11:11.330 回答