如何转储SoapClient的调试请求? [英] How to dump SoapClient request for debug?

查看:86
本文介绍了如何转储SoapClient的调试请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调试一个使用soap客户端的代码.我在php.net中找到了getLast *方法,但是当我尝试获取最后一个调试请求时,它返回NULL

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

<?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);

?>

代码执行的结果:

$php soap_test.php 

====== REQUEST HEADERS =====
NULL
========= REQUEST ==========
NULL
========= RESPONSE =========
object(stdClass)#2 (1) {
  ["ChangePowerUnitResult"]=>
  float(0.0746)
}

如何获取上一个SoapClient请求的正文和标头的内容?

How to get the content of body and headers of the last SoapClient request?

推荐答案

调试SOAP请求

  1. 使用SOAP扩展

调试SOAP请求的最简单,最好的 * 方法实际上是创建

The easiest and best* way to debug a SOAP request is indeed to create a SOAP extension that logs the raw SOAP request and the raw SOAP response from the Web service or Web service client using the following functions of the SoapClient class:

  • SoapClient::__getLastRequestHeaders
  • SoapClient::__getLastRequest
  • SoapClient::__getLastResponseHeaders
  • SoapClient::__getLastResponse

要使其正常工作,您必须在xdazz提到的跟踪选项打开的情况下创建SoapClient对象:

To make it work, you do have to create your SoapClient object with the trace option turned on, as mentioned by xdazz:

$client = new MySoapClient($wsdlUrl, array('trace' => 1));

,然后运行包装在try-catch块中的SOAP调用:

and then run your SOAP calls wrapped in a try-catch block:

try{
   $result = $client->__SoapCall('routeCase', $params);
}catch (\Exception $e){
   throw new \Exception("Soup request failed! Response: ".$client->__getLastResponse());
}

在PHP中开发SOAP解决方案时,最好在WSDL合同更改时清理PHP tmp文件夹(请参见phpinfo()中的tmp文件夹路径)以强制PHP SoapClient下载WSDL和XSD文件.再次使用缓存文件(直到它们过期).

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 tmp folder path in phpinfo()) to force the PHP SoapClient to download WSDL and XSD files again instead of using the cached files (until they expire).

此外,在开发时设置exceptionscache_wsdl以及soap_version版本等选项非常有用:

Furthermore, it's useful to set options like exceptions and cache_wsdl and the soap_version version whilst developing:

$options = array( 
    'soap_version'=>SOAP_1_2, 
    'exceptions'=>false, 
    'trace'=>1, 
    'cache_wsdl'=>WSDL_CACHE_NONE 
);

* 使用SOAP扩展进行调试的缺点是,证书错误发生在实际请求之前或其他事件之前.因此,当出现某种类型的连接失败时,将无法使用getLastRequest()或getLastResponse().

  1. 使用Xdebug

调试SoapClient的另一个有趣的方法是为Xdebug和您喜欢的IDE设置调试会话cookie

Another interesting option to debug a SoapClient is setting a debug session cookie for Xdebug and your favorite IDE

$client = new SoapClient(
    'http://example.loc/index.php/api/v2_soap/?wsdl'
);
$client->__setCookie('XDEBUG_SESSION', 'NETBEANS');

  1. 使用专门的SOAP跟踪器&调试器

专用SOAP跟踪&调试应用程序也非常有用:除了常见的可疑对象(如 SoapUI )之外,还存在诸如Charles的中间跟踪代理在此处.该方法的缺点是增加了更多的层,因此很可能会引入新的问题,例如握手问题.

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调试器,例如 XML间谍SOAP调试器 SOAPSonar 进行验证和调用.无论如何,SoapUI始终是一个很好的伴侣.

There are also some commercial SOAP Debuggers out that are worth the money, like the XML Spy SOAP Debugger or SOAPSonar which do validation and invocation. In any event, SoapUI is always a good companion.

如果您怀疑网络协议级别存在问题,请尝试 Wireshark (适用于Unix的网络协议分析器)和Windows.

If you suspect there is an issue on the network protocol level try Wireshark, a network protocol analyzer for Unix and Windows.

基本错误信息也可以在PHP日志和Web服务器日志中找到.确保已打开完整的错误日志记录.

Basic error information can also be found in the PHP log and the web server log. Ensure you have turned on full error logging.

这篇关于如何转储SoapClient的调试请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆