Bhavin Rana
Bhavin Rana

Reputation: 1582

Convert CURL XML response into Array in PHP

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>

Upvotes: 1

Views: 2557

Answers (1)

alfallouji
alfallouji

Reputation: 1170

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()).

Upvotes: 1

Related Questions