PinoyStackOverflower
PinoyStackOverflower

Reputation: 5302

PHP Parsing a SOAP XML

I have this code:

$xml = '<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:ServiceQualification">
    <x:Header>
        <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <wsse:UsernameToken>
                <wsse:Username>'.$username.'</wsse:Username>
                <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">'.$password.'</wsse:Password>
            </wsse:UsernameToken>
        </wsse:Security>
    </x:Header>
    <x:Body>
        <urn:AddressSearch>
            <urn:property>
                <urn:unit_no>'.$unit_no.'</urn:unit_no>
                <urn:house_no>'.$house_no.'</urn:house_no>
                <urn:lot_no>'.$lot_no.'</urn:lot_no>
                <urn:street_name>'.$street_name.'</urn:street_name>
                <urn:street_type>'.$street_type.'</urn:street_type>
                <urn:suburb>'.$suburb.'</urn:suburb>
                <urn:state_name>'.$state_name.'</urn:state_name>
                <urn:postcode>'.$postcode.'</urn:postcode>
            </urn:property>
        </urn:AddressSearch>
    </x:Body>
</x:Envelope>';

$response = curl($url,$xml);

$clean_xml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $response);

$xml_response = simplexml_load_string($clean_xml) or die("Error: Cannot create object");
echo "<pre>";
print_r($xml_response);
echo "</pre>";

This is the response:

SimpleXMLElement Object
(
    [Body] => SimpleXMLElement Object
        (
        )

)

I am wondering as to why I am getting a blank response because that shouldn't be the case because I tried doing it in boomerang and postman, I am getting data.

This is the response from postman or boomerang:

<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:ServiceQualification">
    <SOAP-ENV:Body>
        <ns1:AddressSearchResponse xmlns:ns1="urn:ServiceQualification">
            <return xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:ServiceQualificationResponse[1]">
                <item xsi:type="tns:ServiceQualificationResponse">
                    <status_code xsi:type="xsd:string">1</status_code>
                    <property_id xsi:type="xsd:string">253323</property_id>
                    <unit_no xsi:type="xsd:string">Basement</unit_no>
                    <house_no xsi:type="xsd:string">8</house_no>
                    <lot_no xsi:type="xsd:string">3</lot_no>
                    <street_name xsi:type="xsd:string">Harbour</street_name>
                    <street_type xsi:type="xsd:string">Road</street_type>
                    <suburb xsi:type="xsd:string">Hamilton</suburb>
                    <state_name xsi:type="xsd:string">QLD</state_name>
                    <postcode xsi:type="xsd:string">4007</postcode>
                    <estate_name xsi:type="xsd:string">Hamilton Harbour</estate_name>
                    <stage xsi:type="xsd:string"></stage>
                    <property_class xsi:type="xsd:string">Class 3</property_class>
                </item>
            </return>
        </ns1:AddressSearchResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Is there a problem with my code that I am getting empty body results?

Your help is appreciated. Thanks.

Upvotes: 1

Views: 2763

Answers (1)

Sahil Gulati
Sahil Gulati

Reputation: 15141

Here i am using DOMDocument to parse soap response.

Try this code snippet here

<?php
ini_set('display_errors', 1);
$string='<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:ServiceQualification">
    <SOAP-ENV:Body>
        <ns1:AddressSearchResponse xmlns:ns1="urn:ServiceQualification">
            <return xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:ServiceQualificationResponse[1]">
                <item xsi:type="tns:ServiceQualificationResponse">
                    <status_code xsi:type="xsd:string">1</status_code>
                    <property_id xsi:type="xsd:string">253323</property_id>
                    <unit_no xsi:type="xsd:string">Basement</unit_no>
                    <house_no xsi:type="xsd:string">8</house_no>
                    <lot_no xsi:type="xsd:string">3</lot_no>
                    <street_name xsi:type="xsd:string">Harbour</street_name>
                    <street_type xsi:type="xsd:string">Road</street_type>
                    <suburb xsi:type="xsd:string">Hamilton</suburb>
                    <state_name xsi:type="xsd:string">QLD</state_name>
                    <postcode xsi:type="xsd:string">4007</postcode>
                    <estate_name xsi:type="xsd:string">Hamilton Harbour</estate_name>
                    <stage xsi:type="xsd:string"></stage>
                    <property_class xsi:type="xsd:string">Class 3</property_class>
                </item>
            </return>
        </ns1:AddressSearchResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>';

$domDocument = new DOMDocument();
$domDocument->loadXML($string);
$carriers=array();
$results=$domDocument->getElementsByTagName("item");
foreach($results as $result)
{
    foreach($result->childNodes as $node)
    {
        if($node instanceof DOMElement)
        {
            array_push($carriers, $node->textContent);
        }
    }

}
print_r($carriers);

Output:

Array
(
    [0] => 1
    [1] => 253323
    [2] => Basement
    [3] => 8
    [4] => 3
    [5] => Harbour
    [6] => Road
    [7] => Hamilton
    [8] => QLD
    [9] => 4007
    [10] => Hamilton Harbour
    [11] => 
    [12] => Class 3
)

Upvotes: 1

Related Questions