我正在从事一个需要Java和PHP技术的项目。我使用Java来实现我的业务逻辑和数据源管理,而PHP则帮助使用我使用Java提供的web服务。我的问题:当我使用web服务的响应并且结果包含多个返回集合时,程序运行良好。(http://driesmeers.mamca.be/images/successpage.png)然而,一旦结果只返回一个集体响应,我就会得到如下所示的错误。(http://driesmeers.mamca.be/images/errorpage.png)
这是我的密码。PHP
<?php
try {
$wsdl_url = 'http://localhost:8080/Mamca/MamcaWS?WSDL';
$client = new SOAPClient($wsdl_url);
$params = array(
'projectid' => $_SESSION['projectID'],
);
$return = $client->getAlternativeListByProjectID($params);
if (isset($return->return)) {
foreach ($return->return as $eachItem) {
echo "<tr align='center'>";
echo "<td align='left' class='rowDisplay'> <a href = '#' class = 'rowNumber2'>" . $eachItem->alternativedescription . "</a></td>";
echo "<td align='left' class='rowDisplay'>" . $eachItem->alternativeGroup . "</td>";
echo "<td align='left' class='rowDisplay'>";
echo ($eachItem->status === 1 ? 'Active' : 'Inactive');
echo "</td>";
echo "<td align='center' class='rowDisplay'><img src = '../images/icons/delete.png' longdesc = 'http://.delete' /><img src = '../images/icons/table_edit.png' width = '16' height = '16' /></td>";
echo "</tr>";
}
}
} catch (Exception $e) {
echo "Exception occured: " . $e;
}
?>
<tr id="ajax_response">
</tr>
WS的响应和错误输出:
<?xml version="1.0" encoding="UTF-8"?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<S:Body>
<ns2:getAlternativeListByProjectIDResponse xmlns:ns2="http://services.mamca.be.vub/">
<return>
<alternativeGroup>testt</alternativeGroup>
<alternativedescription>testtt</alternativedescription>
<alternativeid>10670</alternativeid>
<id>19</id>
<projectid>12313</projectid>
<status>1</status>
</return>
</ns2:getAlternativeListByProjectIDResponse>
</S:Body>
</S:Envelope>
如何解决此错误?非常感谢。
当结果是一个单一元素时,SOAPClient默认不返回数组,而是返回对象。
您可以将SOAPClient设置为始终返回一个数组,即使结果是一个元素,也可以在实例化时添加以下参数:
$client = new SOAPClient($wsdl_url ,array(
"features" => SOAP_SINGLE_ELEMENT_ARRAYS
));