Reputation: 93
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...
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>
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
)
) );
Can anyone point me in the right direction with regard to what's required to change the format of the output XML?
Upvotes: 5
Views: 6020
Reputation: 93
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.
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.
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(...
Upvotes: 4