考虑这个示例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.
Run Code Online (Sandbox Code Playgroud)
在最后一行,PHP从您指定的数组中获取参数,并使用WSDL构建要发送的XML请求,然后发送它.
我怎样才能让PHP向我展示它构建的实际XML?
我正在对应用程序进行故障排除,需要查看请求的实际XML.
sha*_*mar 109
使用getLastRequest
.它返回上一个SOAP请求中发送的XML.
echo "REQUEST:\n" . $SOAP->__getLastRequest() . "\n";
Run Code Online (Sandbox Code Playgroud)
请记住,此方法仅在使用trace
选项设置为创建SoapClient对象时才有效TRUE
.因此,在创建对象时,请使用以下代码:
$SOAP = new SoapClient($WDSL, array('trace' => 1));
Run Code Online (Sandbox Code Playgroud)
小智 17
$SOAP = new SoapClient($WSDL, array('trace' => true));
$Response = $SOAP->DoRemoteFunction($Data);
echo "REQUEST:\n" . htmlentities($SOAP->__getLastRequest()) . "\n";
Run Code Online (Sandbox Code Playgroud)
这不会打印最后一个请求,但也会在浏览器中显示xml标签
Qui*_*ant 14
如果您想在不实际建立连接的情况下查看请求,可以覆盖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);
Run Code Online (Sandbox Code Playgroud)
Tor*_*ott 10
扩展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);
}
}
Run Code Online (Sandbox Code Playgroud)
您需要在创建SoapClient时启用跟踪.像这样:
$SOAP = new SoapClient($WSDL, array('trace' => true));
$Data = array('Something'=>'Some String','SomeNumber'=>22);
Run Code Online (Sandbox Code Playgroud)
然后在进行服务调用后调用__getLastRequest方法以查看XML.
$Response = $SOAP->DoRemoteFunction($Data);
echo $SOAP->__getLastRequest();
Run Code Online (Sandbox Code Playgroud)
这将输出请求XML.
更多阅读:http://www.php.net/manual/en/soapclient.getlastrequest.php