1

I am trying to convert a XML response via CURL to an array in PHP. I have tried with the below code but not getting expected result array.

With using simplexml_load_string:

$xml = simplexml_load_string($response);
$json = json_encode($xml);
$arr = json_decode($json,true);
print_r($arr);//giving array () empty 

With using SimpleXMLElement:

$xml = new SimpleXMLElement($response);
print_r($xml); //giving  SimpleXMLElement Object ( )  empty array 

My Curl response :

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">
  <soap:Header>
    <OGHeader transactionID="00239870" timeStamp="2009-02-23T01:55:01.4625+05:30" primaryLangID="E" xmlns="http://webservices.micros.com/og/4.3/Core/">
      <Origin entityID="WEST" systemType="ORS" />
      <Destination entityID="OWS" systemType="WEB" />
    </OGHeader>
    <wsa:Action>http://webservices</wsa:Action>
    <wsa:MessageID>urn:uuid:a9a70c23-3d94-4640-9aac-8ac63694733a</wsa:MessageID>
    <wsa:RelatesTo>urn:uuid:eb565d90-b682-45e9-b18d-c03fa7323019</wsa:RelatesTo>
    <wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
  </soap:Header>
  <soap:Body>
    <CreateBookingResponse xmlns:r="http://webservices" xmlns:hc="http://webservices/" xmlns:c="http://webservices" xmlns="http://webservices">
      <Result resultStatusFlag="FAIL">
        <c:Text>
          <c:TextElement></c:TextElement>
        </c:Text>
        <c:OperaErrorCode>PRIOR_STAY</c:OperaErrorCode>
      </Result>
    </CreateBookingResponse>
  </soap:Body>
</soap:Envelope>

1 Answer 1

1

Try this instead :

<?php
$xml = simplexml_load_string($response);
var_dump($xml->asXML());

Your xml is there as you can see.

Simplexml implements ArrayIterator, therefore you can iterate using foreach or use any of the simpleXmlElement methods to navigate through it (such as children() or xpath()).

1
  • this is printing : <pre class='xdebug-var-dump' dir='ltr'><small>string</small> <font color='#cc0000'>'&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;&#10;&lt;soap:Envelope xmlns:soap=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns:wsa=&quot;http://schemas.xmlsoap.org/ws/2004/08/addressing&quot;&gt;&lt;soap...entityID=&quot;OWS&quot; systemType=&quot;W'...</font> <i>(length=1322)</i> </pre> Commented Mar 9, 2015 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.