laravel从xml api响应中获取数据

时间:2016-07-22 14:31:29

标签: php xml foreach

我是php的新手,正在处理一个我必须处理xml api的项目。我正在使用laravel 5.2框架来创建项目。

我有一个像这样的xml:

    <?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>

现在我想从中获取房间数据,我的代码是$ response是xml字符串:

 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;
    }

它给我这样的数组:

["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"]

我需要这样的数组:

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

我也试图像这样给出foreach的价值:

$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;

    }

但它只给我最后一个参数如下:

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

请帮助....

2 个答案:

答案 0 :(得分:0)

您正在取名字和类型,并将它们放在同一个数组中:

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

缩小,看起来像这样:

$data[] = $name;
$data[] = $type;

您需要做的是将这些作为关联数组添加到$data数组中。这是一种声明性的方式:

$data[] = [
    'Name' => $names->item(0)->nodeValue,
    'Code' => $types->item(0)->nodeValue
];

这里有效:https://eval.in/610157

答案 1 :(得分:0)

这是一种更简单的方法:

安装此软件包:https://github.com/mtownsend5512/xml-to-array

然后就这么简单:

$xml = <<<XML
<?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>
XML;

$collection = collect(xml_to_array($xml)['RoomList']['Room']);

$result = $collection->map(function ($item) {
    return [
        'Name' => $item['RoomTypeName'],
        'Code' => $item['RoomTypeCode'],
    ];
})->toArray();