0

I am creating a web site. In this web site I use a SOAP request to get a response. First of all I I used cURL to send the request and I got the response. But , when I tried to print_r XML response, it gives me this -

SimpleXMLElement Object ( )

Basically what I want to do is, I want to read these XML line by line as I want. Lets say I want to get ConversionId.

So, how can I do this ??

Here is the function I used to send the SOAP request.

public function authticate(){

$url = 'https://webservices.havail.sabre.com';
        $tokengenheaders = array(
            'Content-Type: text/xml'
        );

        $AuthData = '<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:eb="http://www.ebxml.org/namespaces/messageHeader" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsd="http://www.w3.org/1999/XMLSchema">
   <soap-env:Header>
      <eb:MessageHeader soap-env:mustUnderstand="1" eb:version="1.0">
         <eb:From>
            <eb:PartyId />
         </eb:From>
         <eb:To>
            <eb:PartyId />
         </eb:To>
         <eb:CPAId>ID45</eb:CPAId>
         <eb:ConversationId>IdOfConversion</eb:ConversationId>
         <eb:Service>SessionCreate</eb:Service>
         <eb:Action>SessionCreate</eb:Action>
         <eb:MessageData>
            <eb:MessageId>MsgId</eb:MessageId>
            <eb:Timestamp>2017-11-27T09:58:31Z</eb:Timestamp>
         </eb:MessageData>
      </eb:MessageHeader>
      <wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/12/secext" xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/12/utility">
         <wsse:UsernameToken>
            <wsse:Username>Kist</wsse:Username>
            <wsse:Password>Kistlak</wsse:Password>
            <Organization>FSG</Organization>
            <Domain>SN</Domain>
         </wsse:UsernameToken>
      </wsse:Security>
   </soap-env:Header>
   <soap-env:Body>
      <eb:Manifest soap-env:mustUnderstand="1" eb:version="1.0">
         <eb:Reference xlink:href="cid:rootelement" xlink:type="simple" />
      </eb:Manifest>
      <SessionCreateRQ>
         <POS>
            <Source PseudoCityCode="5H7D" />
         </POS>
      </SessionCreateRQ>
      <ns:SessionCreateRQ xmlns:ns="http://www.opentravel.org/OTA/2002/11" />
   </soap-env:Body>
</soap-env:Envelope>';

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://webservices.sabre.com");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $AuthData); // the SOAP request
        curl_setopt($ch, CURLOPT_HTTPHEADER, $tokengenheaders);
        curl_setopt($ch, CURLOPT_VERBOSE, false);
        $soaptoken = curl_exec($ch);
        curl_close($ch);

        //$xmlElement = new SimpleXMLElement($soaptoken, LIBXML_NOCDATA);

        $xml = simplexml_load_string($soaptoken);
        print_r ($xml) ; die();

        return $soaptoken;

    }

Here is the XML response.

<?xml version="1.0" encoding="UTF-8"?>
<soap-env:Envelope
    xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
    <soap-env:Header>
        <eb:MessageHeader
            xmlns:eb="http://www.ebxml.org/namespaces/messageHeader" eb:version="1.0" soap-env:mustUnderstand="1">
            <eb:From>
                <eb:PartyId eb:type="URI"/>
            </eb:From>
            <eb:To>
                <eb:PartyId eb:type="URI"/>
            </eb:To>
            <eb:CPAId>Id234</eb:CPAId>
            <eb:ConversationId>ConversionId</eb:ConversationId>
            <eb:Service eb:type="sabreXML">Session</eb:Service>
            <eb:Action>SessionCreateRS</eb:Action>
            <eb:MessageData>
                <eb:MessageId>MsgId</eb:MessageId>
                <eb:Timestamp>2019-03-11T09:27:32</eb:Timestamp>
                <eb:RefToMessageId>MsgRef</eb:RefToMessageId>
            </eb:MessageData>
        </eb:MessageHeader>
        <wsse:Security
            xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/12/secext">
            <wsse:BinarySecurityToken valueType="String" EncodingType="wsse:Base64Binary">SecToken</wsse:BinarySecurityToken>
        </wsse:Security>
    </soap-env:Header>
    <soap-env:Body>
        <SessionCreateRS
            xmlns="http://www.opentravel.org/OTA/2002/11" version="1" status="Approved">\t
            <ConversationId>ConversionId</ConversationId>
        </SessionCreateRS>
    </soap-env:Body>
</soap-env:Envelope>

PHP Array Response.

[soap-env_Body] => Array
        (
            [stl19_GetReservationRS] => Array
                (
                    [@attributes] => Array
                        (
                            [Version] => 1.19.0
                        )

                    [stl19_Reservation] => Array
                        (
                            [@attributes] => Array
                                (
                                    [numberInParty] => 3
                                    [numberOfInfants] => 1
                                    [NumberInSegment] => 2
                                )

                            [stl19_BookingDetails] => Array
                                (
                                    [stl19_RecordLocator] => XWYBFF
                                    [stl19_CreationTimestamp] => 2019-03-05T02:22:00
                                    [stl19_SystemCreationTimestamp] => 2019-03-05T02:22:00
                                    [stl19_CreationAgentID] => AWS
                                    [stl19_UpdateTimestamp] => 2019-03-05T02:22:06
                                    [stl19_PNRSequence] => 2
                                    [stl19_DivideSplitDetails] => Array
                                        (
                                        )

                                    [stl19_EstimatedPurgeTimestamp] => 2019-08-12T00:00:00
                                    [stl19_UpdateToken] => 3ea61cc7bab6c025877a7014fd52e44ca4a778bc80edab34
                                )

                            [stl19_POS] => Array
                                (
                                    [@attributes] => Array
                                        (
                                            [AirExtras] => false
                                            [InhibitCode] => U
                                        )

                                    [stl19_Source] => Array
                                        (
                                            [@attributes] => Array
                                                (
                                                    [BookingSource] => 5H7D
                                                    [AgentSine] => AWS
                                                    [PseudoCityCode] => 5H7D
                                                    [ISOCountry] => LK
                                                    [AgentDutyCode] => *
                                                    [AirlineVendorID] => AA
                                                    [HomePseudoCityCode] => 5H7D
                                                    [PrimeHostID] => 1B
                                                )

                                        )

                                )

                            [stl19_PassengerReservation] => Array
                                (
                                    [stl19_Passengers] => Array
                                        (
                                        )

                                    [stl19_Segments] => Array
                                        (
                                        )

                                    [stl19_TicketingInfo] => Array
                                        (
                                        )

                                    [stl19_ItineraryPricing] => Array
                                        (
                                        )

                                )

                            [stl19_ReceivedFrom] => Array
                                (
                                )

                            [stl19_EmailAddresses] => Array
                                (
                                )

                        )

                    [or114_PriceQuote] => Array
                        (
                            [PriceQuoteInfo] => Array
                                (
                                    [Reservation] => Array
                                        (
                                            [@attributes] => Array
                                                (
                                                    [updateToken] => eNc:::h3/WwF9UlK9QLSrRp5hEXg==
                                                )

                                        )

                                    [Summary] => Array
                                        (
                                            [NameAssociation] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [@attributes] => Array
                                                                (
                                                                    [firstName] => RANGA MR
                                                                    [lastName] => DASANAYAKA
                                                                    [nameId] => 1
                                                                    [nameNumber] => 1.1
                                                                )

                                                            [PriceQuote] => Array
                                                                (
                                                                    [@attributes] => Array
                                                                        (
                                                                            [latestPQFlag] => true
                                                                            [number] => 1
                                                                            [pricingStatus] => AUTOPRICED
                                                                            [pricingType] => S
                                                                            [status] => A
                                                                            [type] => PQ
                                                                        )

                                                                    [Indicators] => Array
                                                                        (
                                                                            [@attributes] => Array
                                                                                (
                                                                                    [isExpired] => true
                                                                                )

                                                                        )

                                                                    [Passenger] => Array
                                                                        (
                                                                            [@attributes] => Array
                                                                                (
                                                                                    [passengerTypeCount] => 1
                                                                                    [requestedType] => ADT
                                                                                    [type] => ADT
                                                                                )

                                                                        )

                                                                    [ItineraryType] => I
                                                                    [ValidatingCarrier] => AI
                                                                    [Amounts] => Array
                                                                        (
                                                                            [Total] => 25395
                                                                        )

                                                                    [LocalCreateDateTime] => 2019-03-05T13:52:00
                                                                )

                                                        )

                                                    [1] => Array
                                                        (
                                                            [@attributes] => Array
                                                                (
                                                                    [firstName] => IRUSHA MSTR
                                                                    [lastName] => HASANTHA
                                                                    [nameId] => 2
                                                                    [nameNumber] => 2.1
                                                                )

                                                            [PriceQuote] => Array
                                                                (
                                                                    [@attributes] => Array
                                                                        (
                                                                            [latestPQFlag] => true
                                                                            [number] => 2
                                                                            [pricingStatus] => AUTOPRICED
                                                                            [pricingType] => S
                                                                            [status] => A
                                                                            [type] => PQ
                                                                        )

                                                                    [Indicators] => Array
                                                                        (
                                                                            [@attributes] => Array
                                                                                (
                                                                                    [isExpired] => true
                                                                                )

                                                                        )

                                                                    [Passenger] => Array
                                                                        (
                                                                            [@attributes] => Array
                                                                                (
                                                                                    [passengerTypeCount] => 1
                                                                                    [requestedType] => CNN
                                                                                    [type] => CNN
                                                                                )

                                                                        )

                                                                    [ItineraryType] => I
                                                                    [TicketDesignator] => CH25
                                                                    [ValidatingCarrier] => AI
                                                                    [Amounts] => Array
                                                                        (
                                                                            [Total] => 21695
                                                                        )

                                                                    [LocalCreateDateTime] => 2019-03-05T13:52:00
                                                                )

                                                        )

                                                    [2] => Array
                                                        (
                                                            [@attributes] => Array
                                                                (
                                                                    [firstName] => KISALKA MSTR
                                                                    [lastName] => I/1RAJAPAKSE
                                                                    [nameId] => 3
                                                                    [nameNumber] => 3.1
                                                                )

                                                            [PriceQuote] => Array
                                                                (
                                                                    [@attributes] => Array
                                                                        (
                                                                            [latestPQFlag] => true
                                                                            [number] => 3
                                                                            [pricingStatus] => AUTOPRICED
                                                                            [pricingType] => S
                                                                            [status] => A
                                                                            [type] => PQ
                                                                        )

                                                                    [Indicators] => Array
                                                                        (
                                                                            [@attributes] => Array
                                                                                (
                                                                                    [isExpired] => true
                                                                                )

                                                                        )

                                                                    [Passenger] => Array
                                                                        (
                                                                            [@attributes] => Array
                                                                                (
                                                                                    [passengerTypeCount] => 1
                                                                                    [requestedType] => INF
                                                                                    [type] => INF
                                                                                )

                                                                        )

                                                                    [ItineraryType] => I
                                                                    [TicketDesignator] => IN90
                                                                    [ValidatingCarrier] => AI
                                                                    [Amounts] => Array
                                                                        (
                                                                            [Total] => 2008
                                                                        )

                                                                    [LocalCreateDateTime] => 2019-03-05T13:52:00
                                                                )

                                                        )

                                                )

                                        )
Amithash
  • 269
  • 2
  • 4
  • 22

2 Answers2

2

Turns out SOAP responses are not as friendly as XML. You'll need to do a little trickery to get to what you want to achieve:

/**
 * FUNCTION TO MUNG THE XML SO WE DO NOT HAVE TO DEAL WITH NAMESPACE
 * @link https://stackoverflow.com/a/42311833/429071
 */
function mungXML($xml)
{
    $obj = SimpleXML_Load_String($xml);
    if ($obj === FALSE) return $xml;

    // GET NAMESPACES, IF ANY
    $nss = $obj->getNamespaces(TRUE);
    if (empty($nss)) return $xml;

    // CHANGE ns: INTO ns_
    $nsm = array_keys($nss);
    foreach ($nsm as $key)
    {
        // A REGULAR EXPRESSION TO MUNG THE XML
        $rgx
        = '#'               // REGEX DELIMITER
        . '('               // GROUP PATTERN 1
        . '\<'              // LOCATE A LEFT WICKET
        . '/?'              // MAYBE FOLLOWED BY A SLASH
        . preg_quote($key)  // THE NAMESPACE
        . ')'               // END GROUP PATTERN
        . '('               // GROUP PATTERN 2
        . ':{1}'            // A COLON (EXACTLY ONE)
        . ')'               // END GROUP PATTERN
        . '#'               // REGEX DELIMITER
        ;
        // INSERT THE UNDERSCORE INTO THE TAG NAME
        $rep
        = '$1'          // BACKREFERENCE TO GROUP 1
        . '_'           // LITERAL UNDERSCORE IN PLACE OF GROUP 2
        ;
        // PERFORM THE REPLACEMENT
        $xml =  preg_replace($rgx, $rep, $xml);
    }
    return $xml;
}

Then finally we can run this and we're golden:

$plainXML = mungXML($soaptoken);
$jsonResult = json_encode(SimpleXML_Load_String($plainXML, 'SimpleXMLElement', LIBXML_NOCDATA));
$arrayResult = json_decode($jsonResult , true);
print_r($arrayResult);

This was borrowed from a very similar question: How to convert SOAP response to PHP Array?

dearsina
  • 4,774
  • 2
  • 28
  • 34
  • @dearsina - I have tried this and it gives me empty array - Array ( ). What can I do ?? – Amithash Mar 12 '19 at 07:27
  • @NigelRen - have tried this and it gives me empty array - Array ( ). What can I do ?? – Amithash Mar 12 '19 at 07:27
  • I edited my answer to take into account that SOAP data isn't friendly XML. – dearsina Mar 12 '19 at 07:33
  • SOAP data IS XML, nothing unfriendly about it. – Nigel Ren Mar 12 '19 at 07:33
  • @dearsina - Yes , It's working perfectly.. But, how can I view this Array in the web page ?? I have included the Response array in the question. – Amithash Mar 13 '19 at 04:56
  • You call it, like a normal array. For instance, `$arrayResult['soap-env_Body']['stl19_GetReservationRS']['@attributes']['Version']` is the first value from your response in your question. – dearsina Mar 13 '19 at 05:43
1

There are specific SOAP libraries for PHP (although I haven't used any) which may be a better route, but in your example you can get the content (I'm assuming it's the Body and not the Header content) using XPath to find the body tag and then just use the hierarchy of the XML as though it is object syntax...

$body = $xmlElement->xpath("//soap-env:Body");
if ( count($body) > 0 ) {
    echo $body[0]->SessionCreateRS->ConversationId;
}

The [0] is because xpath() returns a list of matching nodes and you should only have the one - so it just picks the first one. You could do something if nothing is found, but that is up to your app to decide if and what to do.

To access the wsse:BinarySecurityToken element, as it has a namespace, you have to register this with the document to be able to use it in an XPath expression, but then you can use the full name to find this...

$xmlElement->registerXPathNamespace("wsse", "http://schemas.xmlsoap.org/ws/2002/12/secext");
$bst = $xmlElement->xpath("//wsse:BinarySecurityToken");
if ( count($bst) > 0 ) {
    echo $bst[0];
}
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55