[英]Change SOAP request format
我正在忙着整理一個SOAP腳本,這個腳本大部分工作正常,但有一個請求無法正常工作,並且被要求更改主機公司請求XML的格式而且我被卡住了...
目前我的XML請求看起來像這樣......
<?xml version="1.0"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://www.???.com/???/">
<env:Body>
<ns1:GetTransactions>
<ns1:Filter>
<ns1:CardId>1234</ns1:CardId>
</ns1:Filter>
<ns1:Range>
<ns1:FirstRow/>
<ns1:LastRow/>
</ns1:Range>
</ns1:GetTransactions>
</env:Body>
</env:Envelope>
但主辦公司要求它看起來像......
<?xml version="1.0"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Body>
<GetTransactions xmlns="http://www.???.com/???/">
<Filter>
<CardId>1234</CardId>
</Filter>
<Range>
<FirstRow/>
<LastRow/>
</Range>
</GetTransactions>
</env:Body>
</env:Envelope>
我的PHP形成請求如下...
$wsdl = 'http://???.com/???/???.asmx?WSDL';
$endpoint = 'http://???.com/???/???.asp';
$soap_client = new SoapClient( $wsdl, array(
'soap_version' => SOAP_1_2,
'trace' => 1,
'exceptions' => 0,
'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
'location' => $endpoint
) );
$get_transactions = $soap_client->GetTransactions( array(
'Filter' => array(
'CardId' => '1234'
),
'Range' => array(
'FirstRow' => NULL,
'LastRow' => NULL
)
) );
任何人都可以指出我在改變輸出XML格式需要什么方面的正確方向?
主機公司的Web服務存在問題。 Web服務應該接受正在發送的格式,因為它是格式正確的XML。
感謝Wrikken的建議,我想出了一個hacky解決方案。 真正的答案是主機公司修復他們的Web服務以接受格式正確的XML請求。
我擴展了SoapClient類,以便在將XML發送到服務器之前對其進行編輯...
$namespace = 'http://www.???.com/???/';
class HackySoapClient extends SoapClient {
function __doRequest( $request, $location, $action, $version, $one_way = 0 ) {
global $namespace;
// Here we remove the ns1: prefix and remove the xmlns attribute from the XML envelope.
$request = str_replace( '<ns1:', '<', $request );
$request = str_replace( '</ns1:', '</', $request );
$request = str_replace( ' xmlns:ns1="' . $namespace . '"', '', $request );
// The xmlns attribute must then be added to EVERY function called by this script.
$request = str_replace( '<Login', '<Login xmlns="' . $namespace . '"', $request );
$request = str_replace( '<GetTransactions', '<GetTransactions xmlns="' . $namespace . '"', $request );
return parent::__doRequest( $request, $location, $action, $version, $one_way = 0 );
}
}
$soap_client = new HackySoapClient( $wsdl, array(...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.