duanpo2037 2012-12-25 11:30
浏览 158
已采纳

如何转储SoapClient请求进行调试?

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

?>

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?

  • 写回答

3条回答 默认 最新

  • donglang2010 2018-06-14 00:11
    关注

    Debug a SOAP request

    1. Using a SOAP extension

    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:

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

    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());
    }
    

    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).

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

    *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.

    1. Using Xdebug

    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. Using specialized 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.

    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.

    If you suspect there is an issue on the network protocol level try Wireshark, a network protocol analyzer for Unix and 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)