【问题标题】:laravel fetch data from xml api responselaravel 从 xml api 响应中获取数据
【发布时间】:2016-07-22 14:31:29
【问题描述】:

我是 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"}

请帮忙....

【问题讨论】:

  • 你能把你的问题简化为一个更简洁的例子吗?很多关于如何提出好问题的方法包括提出minimal, complete and verifiable example。对于有类似问题的其他用户来说会更好,并且更容易回答。

标签: php xml foreach


【解决方案1】:

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

$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

【讨论】:

    【解决方案2】:

    这里有一个更简单的方法:

    安装这个包: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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-01
      • 1970-01-01
      • 2017-12-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 2014-09-16
      • 2019-04-25
      相关资源
      最近更新 更多