我有一个Zend Soap Server并创建了一个操作setUser()。此操作最终将使用所请求的数据并插入新的User对象。因此,我想要一个包含对象值的数组。
示例:
$request = array("firstname" => "John", "lastname" => "Doe");
setUser($request) { ... }
以下请求基本上适用于数组
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="...">
<soapenv:Header/>
<soapenv:Body>
<soap:setUser soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<setArray xsi:type="soapenc:Array" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<item xsi:type="xsd:string">John</item>
<item xsi:type="xsd:string">Doe</item>
</setArray>
</soap:setUser>
</soapenv:Body>
</soapenv:Envelope>
在我的php代码中转储数组时,我只得到值的数字键。
[0] => John
[1] => Doe
有没有办法指定密钥?我已经尝试过了:
<element name="firstname" xsi:type="xsd:string">John</element>
我想实现:
[firstname] => John
[lastname] => Doe
非常感谢。
答案 0 :(得分:0)
我不确定Zend_SOAP_Server的确切解决方案是什么,但我认为您希望使用HashMap类型,而不是数组,这更接近PHP所谓的“数组”(我来了)与NuSOAP类似的问题:数组键被丢弃,或者它们被用作元素名称,这两者都是PHP“数组”类型的无效表示)
您希望最终得到这样的代表:
<hash xmlns:ns2="http://xml.apache.org/xml-soap" xsi:type="ns2:Map">
<item>
<key xsi:type="xsd:string">firstname</key>
<value xsi:type="xsd:string">John</value>
</item>
<item>
<key xsi:type="xsd:string">lastname</key>
<value xsi:type="xsd:string">Doe</value>
</item>
</hash>
大多数SOAP服务器似乎都解码得很好,即使它们的客户端实现不是默认的。
要么是这样,要么您需要更像这样定义自定义文档架构:
<userStruct xsi:type="myNamespace:userStruct">
<firstname xsi:type="xsd:string">John</value></firstname>
<lastname>Doe</lastname>
</userStruct>
就个人而言,我倾向于发现SOAP产生的问题比解决的问题多,但是YMMV。 :)
答案 1 :(得分:0)
您可以从数组中创建stdClass()
,例如:
function toObject($array) {
foreach ($array as $key=>$value)
if (is_array($value))
$array[$key] = toObject($value);
return (object)$array;
}
然后您可以返回该对象,它将在处理服务器响应时导出到XML,如下所示:
<firstname xsi:type="xsd:string">lastname</firstname>
<lastname xsi:type="xsd:string">Doe</lastname>
我正在使用Zend_Soap_Server()
。