我正在忙着整理一个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格式所需的内容,有人能指出正确的方向吗?
答案 0 :(得分:3)
主机公司的网络服务存在问题。 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(...