php – 更改SOAP请求格式
发布时间:2020-12-13 16:52:10 所属栏目:PHP教程 来源:网络整理
导读:我正在忙着整理一个SOAP脚本,这个脚本大部分工作正常,但有一个请求无法正常工作,并且被要求更改主机公司请求 XML的格式而且我被卡住了… 目前我的XML请求看起来像这样…… ?xml version="1.0"?env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envel
|
我正在忙着整理一个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.
Hacky解决方案 感谢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,$one_way = 0 );
}
}
$soap_client = new HackySoapClient( $wsdl,array(...
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
