我实现了一个生成xml响应的soapserver。
首先我有这个php文件:
<?php
class Testwebservice {
public $name;
public $value;
}
function getWebservice($input1, $input2) {
$testwebservices = array();
$testwebservices[0] = new Testwebservice();
$testwebservices[0]->name = $input1->name;
$testwebservices[0]->value = $input1->value;
$testwebservices[1] = new Testwebservice();
$testwebservices[1]->name = $input2->name;
$testwebservices[1]->value = $input2->value;
/**
* Use an ArrayObject instead of a plain array.
*/
$return_array = new ArrayObject();
foreach ($testwebservices as $l) {
// Build a basic return object.
$webservice = new Testwebservice();
$webservice->name = $l->name;
$webservice->value = $l->value;
/**
* Encode each array element with SoapVar. Parameter 5 is the name of the
* XML element you want to use. This only seems to work within
* an ArrayObject.
*/
$webservice = new SoapVar($webservice, SOAP_ENC_OBJECT, null, null, 'return');
$return_array->append($webservice);
}
print_r($return_array);
return $return_array;
}
$url = 'testphp.local/webservice.php';
$service = new SoapServer(null, ['uri' => $url]);
$service->addFunction('getWebservice');
$service->handle();
此文件返回一个数组对象:
stdClass Object
(
[return] => Array
(
[0] => stdClass Object
(
[name] => input1
[value] => 100
)
[1] => stdClass Object
(
[name] => input2
[value] => 200
)
)
)
当我在xml中转换这个对象时,我得到了这个回复:
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns1:RequestResponse>
<ns1:return>
<ns1:return>
<ns1:name>input</ns1:name>
<ns1:value>100</ns1:value>
</ns1:return>
<ns1:return>
<ns1:name>input2</ns1:name>
<ns1:value>200</ns1:value>
</ns1:return>
</ns1:return>
</ns1:RequestResponse>
</soapenv:Body>
</soapenv:Envelope>
我的问题是我想删除第一个标记返回,所以我可以得到像这样的xml响应:
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns1:RequestResponse>
<ns1:return>
<ns1:name>input</ns1:name>
<ns1:value>100</ns1:value>
</ns1:return>
<ns1:return>
<ns1:name>input2</ns1:name>
<ns1:value>200</ns1:value>
</ns1:return>
</ns1:RequestResponse>
</soapenv:Body>
</soapenv:Envelope>