問題描述
問題:有沒有辦法在您實際發(fā)送請求之前查看使用 PHP SoapClient 函數(shù)調(diào)用創(chuàng)建的 XML?
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?
背景:
我是 WSDL 通信的新手,我有一個客戶希望我用 PHP 進行開發(fā),這是一種與用 ASP.NET 編寫的 WSDL 服務進行通信的方式.我已經(jīng)走得很遠了,但是在傳遞復雜類型時遇到了問題.到目前為止,我已經(jīng)嘗試了幾種不同的方法.
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.
1) 設(shè)置單個數(shù)組如$params->Person->name
$params->Person->address
1) Setting up a single array such as $params->Person->name
$params->Person->address
2) 設(shè)置單個數(shù)組 $Person = array('name'=>"joe",'address' = "123");
然后作為參數(shù)"Person" => $Person;和其他一些人.但是每次我收到錯誤
then passing into the call as a param "Person" => $Person; and a few others. But every time I get the error
SoapException:服務器無法處理請求 ---> System.Exception:人是必需的.在服務名稱.
SoapException: Server was unable to process request ---> System.Exception: Person is Required. at service name.
為了進一步排除故障,我想查看正在發(fā)送的 XML 文檔,以查看它是否按照我期望的方式創(chuàng)建了復雜類型.我正在使用 $client = new SoapClient('wsdldoc.asmx?WSDL');
創(chuàng)建服務,使用 $client->CreateUser($params);
和然后嘗試使用 $client->__getLastRequest();
函數(shù)查看它,但它永遠不會進入 __getLastRequest,因為它在調(diào)用 CreateUser($params) 時遇到了致命錯誤.
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).
再次提問:有什么方法可以查看由 CreateUser($params) 調(diào)用創(chuàng)建的 XML 而不實際發(fā)送它并導致致命錯誤
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
推薦答案
前言:為了使用__getLastRequest()
方法成功,您必須在客戶端構(gòu)建時將 'trace' 選項設(shè)置為 true:
Upfront remark: In order to use the __getLastRequest()
method successfully, you have to set the 'trace' option to true on client construction:
$client = new SoapClient('wsdldoc.asmx?WSDL', array('trace' => TRUE));
這樣,您的請求仍將被發(fā)送(因此仍然失敗),但您可以通過調(diào)用$client->之后檢查發(fā)送的xml;__getLastRequest()
.
This way, your request will still be sent (and therefore still fail), but you can inspect the sent xml afterwards by calling $client->__getLastRequest()
.
主要答案:
要在發(fā)送請求之前/不訪問生成的 XML,您需要子類化 SoapClient 以覆蓋 __doRequest()
方法:
To get access to the generated XML before/without sending the request, you'd need to subclass the SoapClient in order to override the __doRequest()
method:
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);
}
}
然后,您將在調(diào)試問題時使用此擴展類而不是原始 SoapClient.
You'd then use this extended class instead of the original SoapClient while debugging your problem.
這篇關(guān)于在發(fā)送請求之前/不發(fā)送請求之前檢查由 PHP SoapClient 調(diào)用創(chuàng)建的 XML的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!