vendredi 22 juillet 2016

laravel fetch data from xml api response

i am new to php and working on a project where i have to deal with xml api. i am using laravel 5.2 framework to create the project.

i have an xml like this:

    <?xml version="1.0" encoding="UTF-8"?>
<HotelListing>
    <HotelCode Currency="INR">1000000681</HotelCode>
    <RoomList>
        <Room>
            <RoomTypeName>Deluxe Cottage AC</RoomTypeName>
            <RoomTypeCode>45000001713</RoomTypeCode>
            <IsActive>True</IsActive>
        </Room>
        <Room>
            <RoomTypeName>Den Cottage AC</RoomTypeName>
            <RoomTypeCode>45000001714</RoomTypeCode>
            <IsActive>True</IsActive>
        </Room>
        <Room>
            <RoomTypeName>Premium Cottage AC</RoomTypeName>
            <RoomTypeCode>45000001715</RoomTypeCode>
            <IsActive>True</IsActive>
        </Room>
        <Room>
            <RoomTypeName>Couple Package  Deluxe Non AC</RoomTypeName>
            <RoomTypeCode>45000012484</RoomTypeCode>
            <IsActive>False</IsActive>
        </Room>
        <Room>
            <RoomTypeName>Couple Package for Den AC Room</RoomTypeName>
            <RoomTypeCode>45000012503</RoomTypeCode>
            <IsActive>False</IsActive>
        </Room>
        <Room>
            <RoomTypeName>Couple Package for PREMIUM AC </RoomTypeName>
            <RoomTypeCode>45000012507</RoomTypeCode>
            <IsActive>False</IsActive>
        </Room>
    </RoomList>
    <RatePlanList>
        <RatePlan IsEditable="True">
            <RoomTypeName>Deluxe Cottage AC</RoomTypeName>
            <RoomTypeCode>45000001713</RoomTypeCode>
            <IsActive>True</IsActive>
            <RatePlanCode>990000001857</RatePlanCode>
            <RatePlanName>APAI</RatePlanName>
            <MealPlan>FREE Breakfast, Lunch and Dinner</MealPlan>
            <LinkedRatePlan IsLinked="False"/>
        </RatePlan>
        <RatePlan IsEditable="True">
            <RoomTypeName>Deluxe Cottage AC</RoomTypeName>
            <RoomTypeCode>45000001713</RoomTypeCode>
            <IsActive>True</IsActive>
            <RatePlanCode>990000012282</RatePlanCode>
            <RatePlanName>CPAI</RatePlanName>
            <MealPlan>FREE Breakfast</MealPlan>
            <LinkedRatePlan IsLinked="False"/>
        </RatePlan>
        <RatePlan IsEditable="True">
            <RoomTypeName>Deluxe Cottage AC</RoomTypeName>
            <RoomTypeCode>45000001713</RoomTypeCode>
            <IsActive>True</IsActive>
            <RatePlanCode>990000044053</RatePlanCode>
            <RatePlanName>MAPAI</RatePlanName>
            <MealPlan>FREE Breakfast and Dinner</MealPlan>
            <LinkedRatePlan IsLinked="False"/>
        </RatePlan>
        <RatePlan IsEditable="True">
            <RoomTypeName>Den Cottage AC</RoomTypeName>
            <RoomTypeCode>45000001714</RoomTypeCode>
            <IsActive>True</IsActive>
            <RatePlanCode>990000001858</RatePlanCode>
            <RatePlanName>APAI</RatePlanName>
            <MealPlan>FREE Breakfast, Lunch and Dinner</MealPlan>
            <LinkedRatePlan IsLinked="False"/>
        </RatePlan>
        <RatePlan IsEditable="True">
            <RoomTypeName>Den Cottage AC</RoomTypeName>
            <RoomTypeCode>45000001714</RoomTypeCode>
            <IsActive>True</IsActive>
            <RatePlanCode>990000012283</RatePlanCode>
            <RatePlanName>CPAI</RatePlanName>
            <MealPlan>FREE Breakfast</MealPlan>
            <LinkedRatePlan IsLinked="False"/>
        </RatePlan>
        <RatePlan IsEditable="True">
            <RoomTypeName>Den Cottage AC</RoomTypeName>
            <RoomTypeCode>45000001714</RoomTypeCode>
            <IsActive>True</IsActive>
            <RatePlanCode>990000044054</RatePlanCode>
            <RatePlanName>MAPAI</RatePlanName>
            <MealPlan>FREE Breakfast and Dinner</MealPlan>
            <LinkedRatePlan IsLinked="False"/>
        </RatePlan>
        <RatePlan IsEditable="True">
            <RoomTypeName>Premium Cottage AC</RoomTypeName>
            <RoomTypeCode>45000001715</RoomTypeCode>
            <IsActive>True</IsActive>
            <RatePlanCode>990000001859</RatePlanCode>
            <RatePlanName>APAI</RatePlanName>
            <MealPlan>FREE Breakfast, Lunch and Dinner</MealPlan>
            <LinkedRatePlan IsLinked="False"/>
        </RatePlan>
        <RatePlan IsEditable="True">
            <RoomTypeName>Premium Cottage AC</RoomTypeName>
            <RoomTypeCode>45000001715</RoomTypeCode>
            <IsActive>True</IsActive>
            <RatePlanCode>990000012284</RatePlanCode>
            <RatePlanName>CPAI</RatePlanName>
            <MealPlan>FREE Breakfast</MealPlan>
            <LinkedRatePlan IsLinked="False"/>
        </RatePlan>
        <RatePlan IsEditable="True">
            <RoomTypeName>Premium Cottage AC</RoomTypeName>
            <RoomTypeCode>45000001715</RoomTypeCode>
            <IsActive>True</IsActive>
            <RatePlanCode>990000044055</RatePlanCode>
            <RatePlanName>MAPAI </RatePlanName>
            <MealPlan>FREE Breakfast and Dinner</MealPlan>
            <LinkedRatePlan IsLinked="False"/>
        </RatePlan>
    </RatePlanList>
</HotelListing>

and now i want to fetch room data from it and my code is where $response is xml string:

 private function hotelDetailsResponse($response)
    {
       $data = [];

        $dom = new \DOMDocument();
        $dom->formatOutput = TRUE;
        $dom->preserveWhiteSpace = FALSE;
        $dom->loadXml($response);

        $Rooms = $dom->getElementsByTagName( "Room" );
        foreach( $Rooms as $Room ) {

            $names = $Room->getElementsByTagName("RoomTypeName");
            $data[] = $names->item(0)->nodeValue;
            $types = $Room->getElementsByTagName("RoomTypeCode");
            $data[] = $types->item(0)->nodeValue;

            }

           return $data;
    }

it gives me array like this:

["Deluxe Cottage AC","45000001713","Den Cottage AC","45000001714","Premium Cottage AC","45000001715","Couple Package  Deluxe Non AC","45000012484","Couple Package for Den AC Room","45000012503","Couple Package for PREMIUM AC ","45000012507"]

where as i need the array like this:

[{"Name":"Couple Package for PREMIUM AC ","Code":"45000012507"}, {"Name":"Couple Package for PREMIUM AC ","Code":"45000012507"}, {"Name":"Couple Package for PREMIUM AC ","Code":"45000012507"}]

i have also tried to give the value in foreach like this:

$Rooms = $dom->getElementsByTagName( "Room" );
foreach( $Rooms as $Room ) {

    $names = $Room->getElementsByTagName("RoomTypeName");
    $data['name'] = $names->item(0)->nodeValue;
    $types = $Room->getElementsByTagName("RoomTypeCode");
    $data['code'] = $types->item(0)->nodeValue;

    }

but its only give me last parameter like below:

{"name":"Couple Package for PREMIUM AC ","code":"45000012507"}

kindly help....



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire