问题描述
我正忙着编写一个 SOAP 脚本,该脚本大部分工作正常,但是有一个请求工作不正常,并且主机公司要求我更改请求 XML 的格式,我卡住了...
I'm busy putting together a SOAP script which for the most part is working properly, however there is one request which wasn't working properly and have been asked to change the format of the request XML by the host company and I'm stuck...
目前我的 XML 请求看起来像这样...
Currently my XML request looks like this...
<?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>
但主办公司要求它看起来像这样......
But the host company has requested that it looks like this...
<?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 如下...
My PHP that forms the request is as follow...
$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 的格式需要什么,有人能指出我正确的方向吗?
Can anyone point me in the right direction with regard to what's required to change the format of the output XML?
推荐答案
主机公司的 Web 服务存在问题.Web 服务应该接受发送的格式,因为它是正确格式化的 XML.
There is a problem with the host company's web service. The web service should accept the format being sent as it is properly formatted XML.
感谢 Wrikken 的建议,我想出了一个 hacky 解决方案.真正的答案是托管公司修复他们的网络服务以接受格式正确的 XML 请求.
With thanks to Wrikken for his suggestions, I've come up with a hacky solution. The real answer would be for the host company to fix their web service to accept properly formatted XML requests.
我扩展了 SoapClient 类,以便我可以在将 XML 发送到服务器之前对其进行编辑...
I extended the SoapClient class so I could edit the XML before it's sent to the server...
$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(...
这篇关于更改 SOAP 请求格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!