PHP xml使用ba.com API

时间:2022-03-18 02:23:08

I retrieve data from ba.com API using Flight Offer Market Affliates in xml format:

我使用xml格式的Flight Offer Market Affliates从ba.com API检索数据:

$url="https://api.ba.com/rest-v1/v1/flightOfferMktAffiliates;departureDateTimeOutbound=".$Fwk->returnTrueDate($_POST['departureDate']).
                    ";locationCodeOriginOutbound=".$_POST['departureMenu'].
                    ";locationCodeDestinationOutbound=".$_POST['destination'].
                    ";departureDateTimeInbound=".$Fwk->returnTrueDate($_POST['returnDate']).
                    ";locationCodeOriginInbound=".$_POST['destination'].
                    ";locationCodeDestinationInbound=".$_POST['departureMenu'].
                    ";cabin=Economy".
                    ";ADT=".$_POST['adults'].
                    ";CHD=".$_POST['children'].
                    ";INF=0".
                    ";format=.xml";
            $response = curl_get( $url );
            $xml = simplexml_load_string( $response );

I receive something like this:

我收到这样的东西:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<OTA_AirLowFareSearchRS xmlns="http://www.opentravel.org/OTA/2003/05">
    <Success xmlns:a="http://www.opentravel.org/OTA/2003/05" xmlns:ns="http://www.ba.com/wsdl/availabilitymanagerv1" xmlns:tns="http://www.ba.com/schema/availabilitymanager/tGetAvailabilityV2"/>
    <PricedItineraries>
        <PricedItinerary SequenceNumber="1">
            <AirItinerary>
                <OriginDestinationOptions>
                    <OriginDestinationOption>
                        <FlightSegment ArrivalDateTime="2015-05-15T11:05:00" DepartureDateTime="2015-05-15T08:25:00" FlightNumber="117" ResBookDesigCode="O">
                            <DepartureAirport LocationCode="LHR" Terminal="5"/>
                            <ArrivalAirport LocationCode="JFK"/>
                            <OperatingAirline CompanyShortName="British Airways"/>
                            <Equipment AirEquipType="744"/>
                            <MarketingAirline Code="BA"/>
                            <TPA_Extensions>
                                <CabinInfo CabinCode="M" CabinName="World Traveller"/>
                            </TPA_Extensions>
                        </FlightSegment>
                    </OriginDestinationOption>
                    <OriginDestinationOption>
                        <FlightSegment ArrivalDateTime="2015-06-15T19:15:00" DepartureDateTime="2015-06-15T07:15:00" FlightNumber="180" ResBookDesigCode="O">
                            <DepartureAirport LocationCode="JFK" Terminal="7"/>
                            <ArrivalAirport LocationCode="LHR"/>
                            <OperatingAirline CompanyShortName="British Airways"/>
                            <Equipment AirEquipType="744"/>
                            <MarketingAirline Code="BA"/>
                            <TPA_Extensions>
                                <CabinInfo CabinCode="M" CabinName="World Traveller"/>
                            </TPA_Extensions>
                        </FlightSegment>
                    </OriginDestinationOption>
                </OriginDestinationOptions>
            </AirItinerary>
...

And this is as far as I got with understanding the above xml:

这就是我理解上面的xml:

foreach($xml->PricedItineraries->PricedItinerary as $item)
            {
                echo "here";
            }

The above returns a lot of "here" messages, meaning I can loop. But after trying all sorts of echo with $item, I couldn't display a thing. Can anyone show me how I can at least get to and DepartureAirport and read variables from them like ArrivalDateTime and LocationCode.

上面返回了很多“here”消息,这意味着我可以循环。但在用$ item尝试各种回声之后,我无法展示一件事。任何人都可以告诉我如何至少可以到达DepartureAirport并从中读取变量,如ArrivalDateTime和LocationCode。

EDIT

Got it working. See answer.

搞定了。见答案。

1 个解决方案

#1


0  

With respect to xml code in the question, this nested foreach will create a table layout and pull data from xml into it.

关于问题中的xml代码,这个嵌套的foreach将创建一个表格布局并将数据从xml拉入其中。

        <table>
            <tr>
                <td><label>Departure date</label></td>
                <td><label>Operator</label></td>
                <td><label>Airport</label></td>
                <td><label>Terminal</label></td>
                <td><label>Flight number</label></td>
                <td><label>Return date</label></td>
                <td><label>Operator</label></td>
                <td><label>Airport</label></td>
                <td><label>Terminal</label></td>
                <td><label>Flight number</label></td>
                <td><label>Cost<label></td>
            </tr>


        $i=0;
        $c=0;
        foreach($xml->PricedItineraries->PricedItinerary as $pricedItinerary)
        {
            echo "<tr>";
            foreach($pricedItinerary->AirItinerary->OriginDestinationOptions->OriginDestinationOption as $origin)
            {
            //each cycle through this foreach fills one half of a given row.
                echo "<td id='cell_".$i.",".$c."'>".$origin->FlightSegment->attributes()->DepartureDateTime."</td>";$c++; //Such heresy, much reference
                echo "<td id='cell_".$i.",".$c."'>".$origin->FlightSegment->OperatingAirline->attributes()->CompanyShortName."</td>";$c++;
                echo "<td id='cell_".$i.",".$c."'>".$origin->FlightSegment->DepartureAirport->attributes()->LocationCode."</td>";$c++;
                echo "<td id='cell_".$i.",".$c."'>".$origin->FlightSegment->DepartureAirport->attributes()->Terminal."</td>";$c++;
                echo "<td id='cell_".$i.",".$c."'>".$origin->FlightSegment->attributes()->FlightNumber."</td>";$c++;
            }
            echo "<td id='cell_".$i.",".$c."'>£".$pricedItinerary->AirItineraryPricingInfo->ItinTotalFare->TotalFare->attributes()->Amount."</td>";
            $c=0;//reset counter to 0 to avoid generating numbers too large
            echo "</tr>";
            $i++;
        }

There are two FlightSegment arrays inside the given xml, one containing a flight to destination and another - a return flight. To create one row of data that contains both FlightSegments, you need another foreach inside the first that will take OriginDestinationOption as argument. After it loops twice, the main foreach will step into another PricedItinerary (or SequenceNumber if you like).

给定xml中有两个FlightSegment数组,一个包含到目的地的航班,另一个包含返回航班。要创建包含两个FlightSegments的一行数据,您需要在第一行中使用OriginDestinationOption作为参数的另一个foreach。在它循环两次后,主foreach将进入另一个PricedItinerary(或序列号,如果你喜欢)。

The $i and $c are used to give each generated cell a unique id. $i iterates with outer loop, $c iterates with every <td> element and is reset at the end of each row.

$ i和$ c用于为每个生成的单元格提供唯一的ID。 $ i使用外部循环迭代,$ c使用每个元素进行迭代,并在每行结束时重置。

#1


0  

With respect to xml code in the question, this nested foreach will create a table layout and pull data from xml into it.

关于问题中的xml代码,这个嵌套的foreach将创建一个表格布局并将数据从xml拉入其中。

        <table>
            <tr>
                <td><label>Departure date</label></td>
                <td><label>Operator</label></td>
                <td><label>Airport</label></td>
                <td><label>Terminal</label></td>
                <td><label>Flight number</label></td>
                <td><label>Return date</label></td>
                <td><label>Operator</label></td>
                <td><label>Airport</label></td>
                <td><label>Terminal</label></td>
                <td><label>Flight number</label></td>
                <td><label>Cost<label></td>
            </tr>


        $i=0;
        $c=0;
        foreach($xml->PricedItineraries->PricedItinerary as $pricedItinerary)
        {
            echo "<tr>";
            foreach($pricedItinerary->AirItinerary->OriginDestinationOptions->OriginDestinationOption as $origin)
            {
            //each cycle through this foreach fills one half of a given row.
                echo "<td id='cell_".$i.",".$c."'>".$origin->FlightSegment->attributes()->DepartureDateTime."</td>";$c++; //Such heresy, much reference
                echo "<td id='cell_".$i.",".$c."'>".$origin->FlightSegment->OperatingAirline->attributes()->CompanyShortName."</td>";$c++;
                echo "<td id='cell_".$i.",".$c."'>".$origin->FlightSegment->DepartureAirport->attributes()->LocationCode."</td>";$c++;
                echo "<td id='cell_".$i.",".$c."'>".$origin->FlightSegment->DepartureAirport->attributes()->Terminal."</td>";$c++;
                echo "<td id='cell_".$i.",".$c."'>".$origin->FlightSegment->attributes()->FlightNumber."</td>";$c++;
            }
            echo "<td id='cell_".$i.",".$c."'>£".$pricedItinerary->AirItineraryPricingInfo->ItinTotalFare->TotalFare->attributes()->Amount."</td>";
            $c=0;//reset counter to 0 to avoid generating numbers too large
            echo "</tr>";
            $i++;
        }

There are two FlightSegment arrays inside the given xml, one containing a flight to destination and another - a return flight. To create one row of data that contains both FlightSegments, you need another foreach inside the first that will take OriginDestinationOption as argument. After it loops twice, the main foreach will step into another PricedItinerary (or SequenceNumber if you like).

给定xml中有两个FlightSegment数组,一个包含到目的地的航班,另一个包含返回航班。要创建包含两个FlightSegments的一行数据,您需要在第一行中使用OriginDestinationOption作为参数的另一个foreach。在它循环两次后,主foreach将进入另一个PricedItinerary(或序列号,如果你喜欢)。

The $i and $c are used to give each generated cell a unique id. $i iterates with outer loop, $c iterates with every <td> element and is reset at the end of each row.

$ i和$ c用于为每个生成的单元格提供唯一的ID。 $ i使用外部循环迭代,$ c使用每个元素进行迭代,并在每行结束时重置。