从 PHP SoapServer 返回一个 PHP 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1174979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Returning a PHP Array from a PHP SoapServer
提问by Alan Storm
I'm relatively new to Soap on the "creating the service side", so appologies in advance for any terminology I'm munging.
我在“创建服务端”方面对 Soap 比较陌生,因此对于我正在使用的任何术语,请提前应用程序。
Is it possible to return a PHP array from a Remote Procedure Soap Service that's been setup using PHP's SoapServer Class?
是否可以从使用 PHP 的 SoapServer 类设置的远程过程 Soap 服务返回 PHP 数组?
I have a WSDL (built by blindly following a tutorial) that, in part, looks something like this
我有一个 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>
The PHP method I'm calling is named query, and looks something like this
我正在调用的 PHP 方法名为 query,看起来像这样
public function query($arg){
$object = new stdClass();
$object->testing = $arg;
return $object;
}
This allows me to call
这允许我打电话
$client = new SoapClient("http://example.com/my.wsdl");
$result = $client->query('This is a test');
and dump of result will look something like
结果转储看起来像
object(stdClass)[2]
public 'result' => string 'This is a test' (length=18)
I want to return a native PHP array/collection from my query method. If I change my query method to return an array
我想从我的查询方法返回一个本机 PHP 数组/集合。如果我更改查询方法以返回数组
public function query($arg) {
$object = array('test','again');
return $object;
}
It's serialized into an object on the client side.
它在客户端被序列化为一个对象。
object(stdClass)[2]
public 'item' =>
array
0 => string 'test' (length=4)
1 => string 'again' (length=5)
This makes sense, as I've specific a xsd:objectas the Result type in my WSDL. I'd like to, if possible, return an native PHP array that's not wrapped in an Object. My instincts say there's a specific xsd:type that will let me accomplish this, but I don't know. I'd also settle for the object being serialized as an ArrayObject.
这是有道理的,因为我xsd:object在我的 WSDL 中指定了 a作为结果类型。如果可能,我想返回一个未包装在对象中的本机 PHP 数组。我的直觉说有一个特定的 xsd:type 可以让我完成这个,但我不知道。我也满足于将对象序列化为ArrayObject.
Don't hold back on schooling me in the technical details os WSDL. I'm trying to get a grasp on the underlying concepts fo
不要阻止在 WSDL 的技术细节方面教我。我正在尝试了解基本概念
回答by lubosdz
Little trick - encode as JSON objects, decode back into recursive associative arrays:
小技巧 - 编码为 JSON 对象,解码回递归关联数组:
$data = json_decode(json_encode($data), true);
回答by Micha? Rudnicki
I used this WSDL generatorto create description file.
我使用这个 WSDL 生成器来创建描述文件。
Returning array of strings is something what my web service does, here's part of 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>
Then API function notifyis defined:
然后notify定义API函数:
<wsdl:operation name="notify">
<wsdl:input message="tns:notifyRequest" />
<wsdl:output message="tns:notifyResponse" />
</wsdl:operation>
回答by hobodave
Alan, why not cast your object as an array when your client receives the response?
Alan,当您的客户收到响应时,为什么不将您的对象转换为数组?
e.g.
例如
(array) $object;
This will convert your stdClass object into an array, there is no measurable overhead to this, and is O(1) in PHP.
这会将您的 stdClass 对象转换为数组,对此没有可测量的开销,并且在 PHP 中为 O(1)。
You may want to try changing the type from xsd:object to soap-enc:Array as well.
您可能还想尝试将类型从 xsd:object 更改为 soap-enc:Array。

