PHP xml 与 ba.com API 合作
PHP xml working with ba.com API
我使用 xml 格式的 Flight Offer Market Afliates 从 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 );
我收到这样的信息:
<?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>
...
这是我对上述内容的理解xml:
foreach($xml->PricedItineraries->PricedItinerary as $item)
{
echo "here";
}
上面returns很多"here"条消息,意思是我可以循环。但是在用 $item 尝试了各种回显之后,我无法显示任何东西。谁能告诉我如何至少到达 DepartureAirport 并从中读取变量,例如 ArrivalDateTime 和 LocationCode。
编辑
成功了。查看答案。
关于问题中的 xml 代码,这个嵌套的 foreach 将创建一个 table 布局并将数据从 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++;
}
在给定的 xml 中有两个 FlightSegment 数组,一个包含飞往目的地的航班,另一个包含 return 航班。要创建包含两个 FlightSegment 的一行数据,您需要在第一个将 OriginDestinationOption 作为参数的 foreach 中创建另一个。循环两次后,主 foreach 将进入另一个 PricedItinerary(如果您愿意,也可以是 SequenceNumber)。
$i
和$c
用于给每个生成的单元格一个唯一的id。 $i
使用外循环进行迭代,$c
使用每个 <td>
元素进行迭代,并在每行末尾重置。
我使用 xml 格式的 Flight Offer Market Afliates 从 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 );
我收到这样的信息:
<?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>
...
这是我对上述内容的理解xml:
foreach($xml->PricedItineraries->PricedItinerary as $item)
{
echo "here";
}
上面returns很多"here"条消息,意思是我可以循环。但是在用 $item 尝试了各种回显之后,我无法显示任何东西。谁能告诉我如何至少到达 DepartureAirport 并从中读取变量,例如 ArrivalDateTime 和 LocationCode。
编辑
成功了。查看答案。
关于问题中的 xml 代码,这个嵌套的 foreach 将创建一个 table 布局并将数据从 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++;
}
在给定的 xml 中有两个 FlightSegment 数组,一个包含飞往目的地的航班,另一个包含 return 航班。要创建包含两个 FlightSegment 的一行数据,您需要在第一个将 OriginDestinationOption 作为参数的 foreach 中创建另一个。循环两次后,主 foreach 将进入另一个 PricedItinerary(如果您愿意,也可以是 SequenceNumber)。
$i
和$c
用于给每个生成的单元格一个唯一的id。 $i
使用外循环进行迭代,$c
使用每个 <td>
元素进行迭代,并在每行末尾重置。