哎呀!
此示例需要
$soapClient = new SoapClient($url, array('trace'=>1));
首先启用跟踪。
(PHP 5, PHP 7, PHP 8)
SoapClient::__getLastResponse — 返回上次SOAP响应
此函数没有参数。
作为XML字符串的上次SOAP响应。
示例 #1 SoapClient::__getLastResponse() 示例
<?php
$client = SoapClient("some.wsdl", array('trace' => 1));
$result = $client->SomeFunction();
echo "Response:\n" . $client->__getLastResponse() . "\n";
?>
为了调试无法正常工作的程序,几乎可以肯定需要在SOAP调用周围包装一个try/catch块。
否则,PHP会在执行此函数之前抛出致命错误。
例如
<?php
$soapClient = new SoapClient($url);
echo htmlentities($soapClient->__getFunctions());
//假设输出为'someFunction'(以及其他)
try {
$results = $soapClient->someFunction(...);
}
catch (SoapFault $soapFault) {
var_dump($soapFault);
echo "Request :<br>", htmlentities($soapClient->__getLastRequest()), "<br>";
echo "Response :<br>", htmlentities($soapClient->__getLastResponse()), "<br>";
}
?>
如果没有try/catch,您只会得到致命错误,并且PHP会在您可以调用__getLastRequest/__getLastResponse之前崩溃。
为了使其更易读
echo "REQUEST:\n" . htmlentities(str_ireplace('><', ">\n<", $client->__getLastRequest())) . "\n";
echo "RESPONSE:\n" . htmlentities(str_ireplace('><', ">\n<", $client->__getLastResponse())) . "\n";
PS:如果您使用\n,则需要将以上语句括在<pre>中。您也可以使用<br />,但这会有点混乱。