在发送请求之前/不发送请求之前检查由 PHP SoapClient 调用创建的 XML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1626423/
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
Inspect XML created by PHP SoapClient call before/without sending the request
提问by Joshua Cook
The question: Is there a way to view the XML that would be created with a PHP SoapClient function call BEFORE you actually send the request?
问题:在实际发送请求之前,有没有办法查看使用 PHP SoapClient 函数调用创建的 XML?
background:
背景:
I am new to WSDL communication, and I have a client who wants me to develop in PHP, a way to communicate with a WSDL service written in ASP.NET. I have gotten pretty far, but am running into an issue when it comes to passing a complex type. I have tried a couple of different things so far.
我是 WSDL 通信的新手,我有一个客户希望我用 PHP 进行开发,这是一种与用 ASP.NET 编写的 WSDL 服务进行通信的方式。我已经走得很远了,但是在传递复杂类型时遇到了一个问题。到目前为止,我已经尝试了几种不同的方法。
1) Setting up a single array such as $params->Person->name$params->Person->address
1)设置单个数组,例如 $params->Person->name$params->Person->address
2) Setting up a single array $Person = array('name'=>"joe",'address' = "123");
2) 设置单个阵列 $Person = array('name'=>"joe",'address' = "123");
then passing into the call as a param "Person" => $Person; and a few others. But every time I get the error
然后作为参数“Person” => $Person 传入调用;和其他一些人。但每次我得到错误
SoapException: Server was unable to process request ---> System.Exception: Person is Required. at service name.
SoapException: 服务器无法处理请求 ---> System.Exception: Person is required。在服务名称。
In order to further the troubleshooting, I would like to see the XML document that is being sent to see if it is creating a complex type in the way I am expecting it to.
I am creating the service using $client = new SoapClient('wsdldoc.asmx?WSDL');calling it with $client->CreateUser($params);and then trying to see it using the function $client->__getLastRequest();but it never makes it to the __getLastRequest because it hits a fatal error when calling CreateUser($params).
为了进一步排除故障,我想查看正在发送的 XML 文档,以查看它是否按照我期望的方式创建了复杂类型。我正在使用$client = new SoapClient('wsdldoc.asmx?WSDL');调用它来创建服务$client->CreateUser($params);,然后尝试使用该函数查看它,$client->__getLastRequest();但它从未进入 __getLastRequest,因为它在调用 CreateUser($params) 时遇到了致命错误。
The question again: Is there any way to view the XML created by the CreateUser($params) call WITHOUT actually sending it and causing a fatal error
再次提问:有没有办法查看由 CreateUser($params) 调用创建的 XML 而不实际发送它并导致致命错误
回答by Henrik Opel
Upfront remark:In order to use the __getLastRequest()method successfully, you have to set the 'trace' option to true on client construction:
前言:为了__getLastRequest()成功使用该方法,您必须在客户端构建时将 'trace' 选项设置为 true:
$client = new SoapClient('wsdldoc.asmx?WSDL', array('trace' => TRUE));
This way, your request will still be sent(and therefore still fail), but you can inspect the sent xml afterwardsby calling $client->__getLastRequest().
这样,您的请求仍将被发送(因此仍然失败),但您可以在之后通过调用.xml 来检查发送的 xml $client->__getLastRequest()。
Main answer:
主要回答:
To get access to the generated XML before/withoutsending the request, you'd need to subclass the SoapClient in order to override the __doRequest()method:
要在发送请求之前/不发送请求之前访问生成的 XML ,您需要子类化 SoapClient 以覆盖该__doRequest()方法:
class SoapClientDebug extends SoapClient
{
public function __doRequest($request, $location, $action, $version, $one_way = 0) {
// Add code to inspect/dissect/debug/adjust the XML given in $request here
// Uncomment the following line, if you actually want to do the request
// return parent::__doRequest($request, $location, $action, $version, $one_way);
}
}
You'd then use this extended class instead of the original SoapClient while debugging your problem.
然后,您可以在调试问题时使用此扩展类而不是原始 SoapClient。
回答by andrewembassy
I found this thread while working on the same problem, and was bummed because I was using classes that already extended the SoapClient() class and didn't want to screw around with it too much. However if you add the "exceptions"=>0 tag when you initiate the class, it won't throw a Fatal Error (though it will print an exception):
我在解决同一问题时发现了这个线程,并且很沮丧,因为我使用的类已经扩展了 SoapClient() 类并且不想过多地使用它。但是,如果您在启动类时添加“exceptions”=>0 标记,则不会抛出致命错误(尽管它会打印异常):
SoapClient($soapURL,array("trace" => 1,"exceptions"=>0));
Doing that allowed me to run __getLastRequest() and analyze the XML I was sending.
这样做允许我运行 __getLastRequest() 并分析我发送的 XML。
回答by BrandonCS
I don't believe there is a way that you'll be able to see any XML that's being created... mainly because the function is failing on it's attempt to create/pass it.
我不相信有一种方法可以让您看到正在创建的任何 XML……主要是因为该函数在尝试创建/传递它时失败了。
Not sure if you tried already, but if you're having trouble trying to decide what exactly you need to pass into the function you could use:
不确定您是否已经尝试过,但是如果您在尝试确定需要传递给函数的确切内容时遇到问题,则可以使用:
$client->__getTypes();
$client->__getTypes();
http://us3.php.net/manual/en/soapclient.gettypes.php
http://us3.php.net/manual/en/soapclient.gettypes.php
Hope this helps!
希望这可以帮助!

