Link to home
Start Free TrialLog in
Avatar of James Murphy
James MurphyFlag for Australia

asked on

How to access response XML inside soap packet using PHP.

Hi Experts,

Firstly thank you for taking a look at my question!

Using PHP i need to access the xml body inside a soap packet.

the soap response looks like this:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <Response   Version="1.0" Environment="T" xmlns="http://www.dnb.com.au/Schema/CCB/1.0">
            <ConsumerHeader SegId="ERHD01" InternalReference="M20190912095903-023-000238" EnquiryType="1" UniqueCustomerReference="Test" ConsumerId="2190130" Country="AU" ProductCode="AMD3" NewFileCreated="0" IsMinor="0" HasSummary="1" HasJudgment="1" HasBankruptcy="1" HasSummons="1" HasOtherPublicRecord="0" HasDirector="0" HasFileNote="0" HasDefault="1" HasEnquiry="1" HasError="0" HasWarning="0">
                <EnquiryDate Year="2019" Month="9" Day="12" />
                <PersonName>
                    <FirstName>FirstTest</FirstName>
                    <Surname>LastTest</Surname>
                </PersonName>
            </ConsumerHeader>
            <BestMatch MatchScore="768">
                <IndividualDetail SegId="ERID01">
                    <DateFirstRecorded Year="2003" Month="6" Day="10" />
                    <Person Sex="U">
                        <PersonName>
                            <FirstName>FirstTest</FirstName>
                            <Surname>LastTest</Surname>
                        </PersonName>
                        <DateOfBirth>
                            <Date Year="1948" Month="11" Day="26" />
                        </DateOfBirth>
                    </Person>
                    <ContactDetails>
                        <AddressDetails Current="Yes" PriorAddress="No" Mailing="No">
                            <StreetNo>4</StreetNo>
                            <Street Type="Court">Windsor</Street>
                            <City>Goonellabah</City>
                            <State>QLD</State>
                            <Postcode>3480</Postcode>
                            <DateRecorded Year="2014" Month="2" Day="7" />
                            <DateLastRecorded Year="2018" Month="12" Day="6" />
                        </AddressDetails>
                    </ContactDetails>
                    <Employment>
                        <Employer>Empname0086</Employer>
                    </Employment>
                </IndividualDetail>
  </BestMatch>
             <Trailer SegId="ERTR01">
                 <Disclosure>
 Disclosure Notice
 
 Blah Blah
 </Disclosure>
             </Trailer>
         </Response>
     </soap:Body>
 </soap:Envelope>

Open in new window


Could someone point me in the right direction?  ie how to access bestmatch/IndividualDetail/PersonName/FirstName?

many thanks for any help you can give me!
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

If you're dealing with a PHP script that is RECEIVING an incoming SOAP request, then you need to first read the data via the special "php://input" pipe:

$xml = file_get_contents("php://input");

Open in new window


From there, you can use SimpleXML to read the XML structure:
https://www.php.net/manual/en/simplexml.examples-basic.php
Avatar of James Murphy

ASKER

I tried this,

$sop = SimpleXML_Load_String($response);
$str = (string)$sop->soapBody->Response;
      echo "<br /><br />";
echo print_r($str);

but still no joy.
Hi gr8gonzo,

I tried this as the last part of the curl but I don't think it is right.

$response = curl_exec($curl);
$err = curl_error($curl);
$client_data = file_get_contents("php://input");
curl_close($curl);
I don't understand what you're trying to do with cURL.

cURL is a client library - it's used for communicating with a remote server. So that's OUTBOUND data (-from- your script to some other place)

The php://input pipe is used to access all the raw data that is sent to the PHP script when it is first loaded, so that's INBOUND data (from some other place -to- your script).

So are you trying to create and send a SOAP request to a server, or are you trying to create a server that accepts a SOAP request?
Ah wait, I think I see what you're doing. You're using cURL to send a SOAP request and you're trying to parse the response. Sorry - I somehow missed that when I was reading the question.

So in that case, don't worry about php://input - that isn't relevant here. Assuming the above is true, then this line:
$response = curl_exec($curl);

Open in new window

...should put the SOAP XML data into $response.
I have created a curl request that submits a soap packet and gets back the response as per above.
I had that, but was getting bogged down on the soap part of the header,

I think i just worked it out though.

I think using this:

$array = json_decode(json_encode($response), true);
$sop = SimpleXML_Load_String($array);

I can now access the response without the soap wrapper.

many thanks!
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you for your assistance, it was greatly appreciated!