php 如何转储 SoapClient 请求以进行调试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14030228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to dump SoapClient request for debug?
提问by Denis Kreshikhin
I need to debug a code wich use soap client. I found getLast* methods in php.net, but when I try to get last request for debug it returns NULL
我需要调试使用soap客户端的代码。我在 php.net 中找到了 getLast* 方法,但是当我尝试获取最后一个调试请求时,它返回 NULL
<?php
$client = new SoapClient("http://www.webservicex.net/ConverPower.asmx?WSDL");
$response = $client->ChangePowerUnit(array(
"PowerValue" => 100,
"fromPowerUnit" => "horsepower",
"toPowerUnit" => "megawatts"
));
echo "====== REQUEST HEADERS =====" . PHP_EOL;
var_dump($client->__getLastRequestHeaders());
echo "========= REQUEST ==========" . PHP_EOL;
var_dump($client->__getLastRequest());
echo "========= RESPONSE =========" . PHP_EOL;
var_dump($response);
?>
The result of code execution:
代码执行结果:
$php soap_test.php
====== REQUEST HEADERS =====
NULL
========= REQUEST ==========
NULL
========= RESPONSE =========
object(stdClass)#2 (1) {
["ChangePowerUnitResult"]=>
float(0.0746)
}
How to get the content of body and headers of the last SoapClient request?
如何获取最后一个 SoapClient 请求的正文和标题的内容?
采纳答案by wp78de
Debug a SOAP request
调试 SOAP 请求
- Using a SOAP extension
- 使用 SOAP 扩展
The easiest and best*way to debug a SOAP request is indeed to create a SOAP extensionthat logs the raw SOAP request and the raw SOAP response from the Web service or Web service client using the following functions of the SoapClientclass:
最简单和最*的方式来调试SOAP请求的确是创建一个SOAP扩展,它记录的原始SOAP请求,并从使用以下功能的Web服务或Web服务客户端的原始SOAP响应SoapClient的类:
- SoapClient::__getLastRequestHeaders
- SoapClient::__getLastRequest
- SoapClient::__getLastResponseHeaders
- SoapClient::__getLastResponse
- SoapClient::__getLastRequestHeaders
- SoapClient::__getLastRequest
- SoapClient::__getLastResponseHeaders
- SoapClient::__getLastResponse
To make it work, you do have to create your SoapClient object with the trace option turned on, as mentioned by xdazz:
要使其工作,您必须在启用跟踪选项的情况下创建 SoapClient 对象,如 xdazz 所述:
$client = new MySoapClient($wsdlUrl, array('trace' => 1));
and then run your SOAP calls wrapped in a try-catch block:
然后运行包含在 try-catch 块中的 SOAP 调用:
try{
$result = $client->__SoapCall('routeCase', $params);
}catch (\Exception $e){
throw new \Exception("Soup request failed! Response: ".$client->__getLastResponse());
}
When developing SOAP solutions in PHP it is also a good idea to clean the PHP tmp folder when your WSDL contract changes (see your tmpfolder path in phpinfo()) to force the PHP SoapClient to download WSDL and XSD files again instead of using the cached files (until they expire).
在 PHP 中开发 SOAP 解决方案时,最好在 WSDL 合同更改时清理 PHP tmp 文件夹(请参阅 中的tmp文件夹路径phpinfo())以强制 PHP SoapClient 再次下载 WSDL 和 XSD 文件,而不是使用缓存文件(直到它们会过期)。
Furthermore, it's useful to set options like exceptionsand cache_wsdland the soap_versionversion whilst developing:
此外,在开发过程中设置像exceptions和cache_wsdl和soap_version版本这样的选项很有用:
$options = array(
'soap_version'=>SOAP_1_2,
'exceptions'=>false,
'trace'=>1,
'cache_wsdl'=>WSDL_CACHE_NONE
);
*The downside of debugging using a SOAP extension is that certificate-errors happen before the actual request or something. So it's not possible to use getLastRequest() or getLastResponse() when there is a connection failure of some kind.
*使用 SOAP 扩展进行调试的缺点是证书错误发生在实际请求之前。因此,当出现某种连接失败时,不可能使用 getLastRequest() 或 getLastResponse()。
- Using Xdebug
- 使用 Xdebug
Another interesting option to debug a SoapClient is setting a debug session cookie for Xdebug and your favorite IDE
调试 SoapClient 的另一个有趣选项是为 Xdebug 和您最喜欢的 IDE 设置调试会话 cookie
$client = new SoapClient(
'http://example.loc/index.php/api/v2_soap/?wsdl'
);
$client->__setCookie('XDEBUG_SESSION', 'NETBEANS');
- Using specialized SOAP Tracer & Debugger
- 使用专门的 SOAP Tracer & Debugger
Specialized SOAP Trace & Debug apps are very useful too: In addition to usual suspects like SoapUI, there are also intermediate tracing proxies like Charles as described here. The downside of this method is that more layers are added and therefore are likely to introduce new problems, e.g. handshake problems.
专门的 SOAP Trace & Debug 应用程序也非常有用:除了像SoapUI这样的常见问题之外,还有这里描述的像 Charles 这样的中间跟踪代理。这种方法的缺点是添加了更多层,因此可能会引入新问题,例如握手问题。
There are also some commercial SOAP Debuggers out that are worth the money, like the XML Spy SOAP Debuggeror SOAPSonarwhich do validation and invocation. In any event, SoapUI is always a good companion.
还有一些值得花钱的商业 SOAP 调试器,例如XML Spy SOAP Debugger或SOAPSonar,它们进行验证和调用。无论如何,SoapUI 始终是一个好伙伴。
If you suspect there is an issue on the network protocol level try Wireshark, a network protocol analyzer for Unix and Windows.
如果您怀疑网络协议级别存在问题,请尝试Wireshark,这是一种适用于 Unix 和 Windows 的网络协议分析器。
Logs, logs, logs
日志,日志,日志
Basic error information can also be found in the PHP log and the web server log. Ensure you have turned on full error logging.
基本错误信息也可以在 PHP 日志和 Web 服务器日志中找到。确保您已打开完整的错误日志记录。
回答by xdazz
These functionsonly works if the SoapClient object was created with the trace option set to TRUE.
这些函数仅在创建 SoapClient 对象时跟踪选项设置为 TRUE时才有效。
Try:
尝试:
$client = new SoapClient("http://www.webservicex.net/ConverPower.asmx?WSDL", array('trace' => 1));
回答by MSS
I just wrap it up for action:
我只是把它包装起来以备行动:
$client = new \SoapClient("http://127.0.0.1/services/wsdl2",array('trace' => 1,));
try{
$result = $client->__SoapCall('routeCase', $params);
}catch (\Exception $e){
throw new \Exception("Soup Request Failed! Response:\n".$client->__getLastResponse());
}

