Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Processing SOAP response

Tags:

php

soap

I am attempting to process a SOAP response from First Data's Global Gateway. I've used SoapClient before, but there is no wsdl - and the company says they do not supply one.

I've tried various other approaches such as SimpleXMLElement based on examples found here and in the PHP manual but I can't get anything to work. I suspect namespaces are part of my problem. Can anyone suggest an approach or point me to a similar example - my Google efforts have been fruitless to date.

Using PHP 5.

Partial SOAP Response (with all the HTML header stuff that precedes it stripped off) looks like this:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">

<SOAP-ENV:Header/>

<SOAP-ENV:Body>

<fdggwsapi:FDGGWSApiOrderResponse xmlns:fdggwsapi="http://secure.linkpt.net/fdggwsapi/schemas_us/fdggwsapi">

<fdggwsapi:CommercialServiceProvider/>

<fdggwsapi:TransactionTime>Thu Nov 29 17:03:18 2012</fdggwsapi:TransactionTime>

<fdggwsapi:TransactionID/>

<fdggwsapi:ProcessorReferenceNumber/>

<fdggwsapi:ProcessorResponseMessage/>

<fdggwsapi:ErrorMessage>SGS-005005: Duplicate transaction.</fdggwsapi:ErrorMessage>

<fdggwsapi:OrderId>A-e833606a-5197-45d6-b990-81e52df41274</fdggwsapi:OrderId>
...

<snip>

I also need to be able to determine if a SOAP fault was signalled. XML for that looks like this:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<SOAP-ENV:FaultX>
<faultcode>SOAP-ENV:Client</faultcode>
<faultstring xml:lang="en">MerchantException</faultstring>
<detail>
cvc-pattern-valid: Value '9999185.00' is not facet-valid with respect to pattern '([1-9]([0-9]{0,3}))?[0-9](\.[0-9]{1,2})?' for type '#AnonType_ChargeTotalAmount'.
cvc-type.3.1.3: The value '9999185.00' of element 'v1:ChargeTotal' is not valid.
</detail>
</SOAP-ENV:FaultX>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Using Mr. Code's answer I have been able to retrieve the data from the non-fault responses. But I need to determine what type of packet I am dealing with and extract data from both types. It would be so much easier if only they would supply a wsdl!

like image 237
JonP Avatar asked Nov 30 '12 17:11

JonP


People also ask

What is a SOAP message?

A SOAP message is an ordinary XML document containing the following elements: An Envelope element that identifies the XML document as a SOAP message. A Header element that contains header information.

How to check if SOAP response has proper XML format?

1.- first one is to see if SOAP response has proper XML format, you can use the following expression to evaluate it: If malformed xml, flow will fail. If proper xml, you can use xpath () based expression to read XML nodes inside.

What is soap and how does it work?

SOAP provides a way to communicate between applications running on different operating systems, with different technologies and programming languages. A SOAP message is an ordinary XML document containing the following elements:

What are the syntax rules for sending a SOAP message?

Syntax Rules. Here are some important syntax rules: A SOAP message MUST be encoded using XML. A SOAP message MUST use the SOAP Envelope namespace. A SOAP message MUST use the SOAP Encoding namespace. A SOAP message must NOT contain a DTD reference. A SOAP message must NOT contain XML Processing Instructions.


1 Answers

Your response can be parsed with SimpleXML, here's an example. Notice I am passing the namespace URL to children() to access the elements.

$obj = simplexml_load_string($xml);

$response = $obj->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://secure.linkpt.net/fdggwsapi/schemas_us/fdggwsapi')->FDGGWSApiOrderResponse;

echo $response->TransactionTime . "\n";
echo $response->ErrorMessage;

Outputs

Thu Nov 29 17:03:18 2012
SGS-005005: Duplicate transaction.

Codepad Demo

Edit: The SoapFault response can be parsed like below. It outputs the fault string and details, or 'No fault found':

if($obj->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://schemas.xmlsoap.org/soap/envelope/') && isset($obj->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://schemas.xmlsoap.org/soap/envelope/')->children()->faultcode))
{
    $fault = $obj->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://schemas.xmlsoap.org/soap/envelope/')->children();

    // soap fault
    echo $fault->faultstring;
    echo $fault->detail;
}
else
{
    echo 'No fault found, do normal parsing...';
}
like image 140
MrCode Avatar answered Oct 03 '22 09:10

MrCode