How to convert curl xml response into array in php?

10,567

Why convert it to an array at all? PHP is pretty good with XML. Just load up the XML in SimpleXML or a DOMDocument and use it directly.

$xml = new SimpleXMLElement($response);
echo $xml->properties->property['HotelName'];
Share:
10,567
Hearaman - Freelancer
Author by

Hearaman - Freelancer

Updated on November 21, 2022

Comments

  • Hearaman - Freelancer
    Hearaman - Freelancer over 1 year

    I am using curl in my project. When i am sending request, the response is coming in the form of XML as bellow. How can i convert this xml into array to present in the web page.

     <?xml version="1.0" encoding="UTF-8"?>
        <OTA_HotelAvailRS Version="1.0">
          <Success Id="140948"/>
           <Properties>
             <Property HotelCityCode="ELS" HotelCode="18918" HotelName="Premier Hotel Regent - Demo">
               <RelativePosition Direction="" Distance="0" DistanceUnitName=""/>
               <EssentialInfo>
               </EssentialInfo>
               <RoomStays>
                    <RoomStay>
                         <RatePlans>
                              <RatePlan RatePlanCode="50010"/>
                         </RatePlans>
                         <RoomRates>
                              <RoomRate>
                                   <Rates>
                                        <Rate EffectiveDate="2011-10-14" ExpireDate="2011-10-15">
                                             <Base Amount="114.00" CurrencyCode="EUR"/>
    
                                             <RateDescription Adults="1" Availability="A" Children="0" RoomNum="1">
                                                  Standard
                                             </RateDescription>
                                        </Rate>
                                   </Rates>
                              </RoomRate>
                         </RoomRates>
                         <Meals Description="Breakfast Buffet" MealType="Breakfast"/>
                    </RoomStay>
               </RoomStays>
               <Promotions/>
               <AdditionalInfo>
                    <HotelStarDetail rating="3"/>
                    <HotelImages>
                         <HotelImage Type="" URL="http://image1.urlforimages.com/1204258guest.jpg"/>
                    </HotelImages>
                    <HotelDescription>
                         <LongDescription> description</LongDescription>
                    </HotelDescription>
               </AdditionalInfo>
          </Property>          
     </Properties>
    

  • Hearaman - Freelancer
    Hearaman - Freelancer almost 13 years
    I have edited above xml code. It contains some big code. Now how can i display all HotelName.
  • Sander Marechal
    Sander Marechal almost 13 years
    @Hearaman: I don't see your edited XML code. But you can simply foreach over an XML tree suing SimpleXML. See example #4 at nl.php.net/manual/en/simplexml.examples-basic.php
  • Hearaman - Freelancer
    Hearaman - Freelancer almost 13 years
    Thanks for your tip and suggestion
  • Hearaman - Freelancer
    Hearaman - Freelancer almost 13 years
    To achieve this task with given lines, we should keep that response between $xml=<<< XML and XML; How to keep my response between them. If i keep my response directly in SimpleXMLElement() output is not coming.