如何查看 PHP SOAP 客户端类生成的实际 XML?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3572414/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:15:58  来源:igfitidea点击:

How do I see the actual XML generated by PHP SOAP Client Class?

phpxmlsoap

提问by Nick

Consider this example SOAP Client script:

考虑这个示例 SOAP 客户端脚本:

$SOAP = new SoapClient($WDSL); // Create a SOAP Client from a WSDL

// Build an array of data to send in the request.
$Data = array('Something'=>'Some String','SomeNumber'=>22); 

$Response = $SOAP->DoRemoteFunction($Data); // Send the request.

On the last line, PHP takes the arguments from the array you specified, and, using the WSDL, builds the XML request to send, then sends it.

在最后一行,PHP 从您指定的数组中获取参数,并使用 WSDL 构建要发送的 XML 请求,然后发送它。

How can I get PHP to show me the actual XML it's built?

我怎样才能让 PHP 向我展示它构建的实际 XML?

I'm troubleshooting an application and need to see the actual XML of the request.

我正在对应用程序进行故障排除,需要查看请求的实际 XML。

回答by shamittomar

Use getLastRequest. It returns the XML sent in the last SOAP request.

使用getLastRequest. 它返回在最后一个 SOAP 请求中发送的 XML。

echo "REQUEST:\n" . $SOAP->__getLastRequest() . "\n";

And remember, this method works only if the SoapClient object was created with the traceoption set to TRUE. Therefore, when creating the object, use this code:

请记住,此方法仅在创建 SoapClient 对象且trace选项设置为时才有效TRUE。因此,在创建对象时,请使用以下代码:

$SOAP = new SoapClient($WDSL, array('trace' => 1));

回答by Shankky

$SOAP = new SoapClient($WSDL, array('trace' => true));

$Response = $SOAP->DoRemoteFunction($Data);

echo "REQUEST:\n" . htmlentities($SOAP->__getLastRequest()) . "\n";

this will not print the last request but also make the xml tags visible in the browser

这不会打印最后一个请求,但也会使 xml 标签在浏览器中可见

回答by Quinn Comendant

If you'd like to view the request without actually making a connection, you can override SoapClient's __doRequestmethod to return the XML:

如果您想查看请求而不实际建立连接,您可以覆盖 SoapClient 的__doRequest方法以返回 XML:

class DummySoapClient extends SoapClient {
    function __construct($wsdl, $options) {
        parent::__construct($wsdl, $options);
    }
    function __doRequest($request, $location, $action, $version, $one_way = 0) {
        return $request;
    }
}
$SOAP = new DummySoapClient('http://example.com/?wsdl', array('trace' => true));
echo $SOAP->GetRequestDetail($params);

回答by TorranceScott

Extending Quinn's answer, you can also just log the request before you perform the request.

扩展 Quinn 的回答,您也可以在执行请求之前记录请求。

class SoapClientDebug extends SoapClient
{

public function __doRequest($request, $location, $action, $version, $one_way = 0)
{
    error_log("REQUEST:\n" .$request . "\n");
    error_log("LOCATION:\n" .$location . "\n");
    error_log("ACTION:\n" .$action . "\n");
    error_log("VERSION:\n" .$version . "\n");
    error_log("ONE WAY:\n" .$one_way . "\n");

    return parent::__doRequest($request, $location, $action, $version, $one_way);
}
}

回答by Ezequiel Muns

You need to enable tracing when you create your SoapClient. Like so:

您需要在创建 SoapClient 时启用跟踪。像这样:

$SOAP = new SoapClient($WSDL, array('trace' => true));

$Data = array('Something'=>'Some String','SomeNumber'=>22); 

Then call the __getLastRequest method after you've made a service call to see the XML.

然后在进行服务调用以查看 XML 后调用 __getLastRequest 方法。

$Response = $SOAP->DoRemoteFunction($Data);
echo $SOAP->__getLastRequest();

This will output the request XML.

这将输出请求 XML。

More reading: http://www.php.net/manual/en/soapclient.getlastrequest.php

更多阅读:http: //www.php.net/manual/en/soapclient.getlastrequest.php

回答by Pratik Bhatt

if you are running the client locally, Fiddleris a great implementation agnostic way of looking at the messages on the wire.

如果您在本地运行客户端,Fiddler是查看线路上消息的一种很好的实现不可知的方式。

If you are running it remotely then you could use something like Apache TCPMONStandaloneor through eclipse*

如果您远程运行它,那么您可以使用Apache TCPMON Standalone 之类的东西或通过eclipse*

*just linking to the first hit from Google

*只是链接到来自谷歌的第一个点击

回答by Gino Pane

The problem with Quinn Comendant'sanswer, that $requestfrom __doRequest()will then be processed by __call()and the user will see an array of parameters instead of real xml request. To prevent this, such workaround can be used:

这个问题奎因Comendant的答案,这$request__doRequest()随后将由处理__call(),用户将看到的参数,而不是真正的XML请求的数组。为了防止这种情况,可以使用这样的解决方法:

class DummySoapClient extends SoapClient {
    function __construct($wsdl, $options) {
        parent::__construct($wsdl, $options);
    }

    function __doRequest($request, $location, $action, $version, $one_way = 0) {
        throw new Exception($request);
    }

    function __call($function_name, $arguments)
    {
        try {
            parent::__call($function_name, $arguments);
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }
}

Option traceis not necessary here, because we do not call __getLastRequest()or other relevant functions.

trace此处不需要选项,因为我们不调用__getLastRequest()或其他相关函数。