从 PHP SoapServer 返回 PHP 数组
我对 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)
这是有道理的,因为我在 WSDL 中指定了 xsd:object
作为结果类型。 如果可能的话,我想返回一个未包装在对象中的本机 PHP 数组。 我的直觉告诉我有一个特定的 xsd:type 可以让我完成此任务,但我不知道。 我还愿意将对象序列化为 ArrayObject 。
请不要吝啬地教我 WSDL 的技术细节。 我正在尝试掌握基本概念
I'm relatively new to Soap on the "creating the service side", so appologies in advance for any terminology I'm munging.
Is it possible to return a PHP array from a Remote Procedure Soap Service that's been setup using PHP's SoapServer Class?
I have a WSDL (built by blindly following a tutorial) that, in part, looks something like this
<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
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
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:object
as 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
.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
小技巧 - 编码为 JSON 对象,解码回递归关联数组:
Little trick - encode as JSON objects, decode back into recursive associative arrays:
我使用这个 WSDL 生成器来创建描述文件。
返回字符串数组是我的 Web 服务所做的事情,这是 WSDL 的一部分:
然后定义 API 函数
notify
:I used this WSDL generator to create description file.
Returning array of strings is something what my web service does, here's part of WSDL:
Then API function
notify
is defined:艾伦,当您的客户端收到响应时,为什么不将您的对象转换为数组呢?
例如,
这会将您的 stdClass 对象转换为数组,对此没有可测量的开销,并且在 PHP 中是 O(1) 。
您可能还想尝试将类型从 xsd:object 更改为soap-enc:Array。
Alan, why not cast your object as an array when your client receives the response?
e.g.
This will convert your stdClass object into an array, there is no measurable overhead to this, and is O(1) in PHP.
You may want to try changing the type from xsd:object to soap-enc:Array as well.