:您可以使用this demo flight database檢查你的JSON無需提交請求真正QPX API。首先你必須做一個正確的錯誤報告,所以日期應該不是過去。我只是閱讀tripOptions。對於每個TripOptions查詢中的切片到服務器,然後腿。
單段
作爲TripInfos服務器的響應。你有旅行分爲兩個要求的片。對於每張幻燈片,您都有一個細分。一段看起來像這樣。
"segment": [{
"kind": "qpxexpress#segmentInfo",
"duration": 175,
"flight": {
"carrier": "TK",
"number": "1638"
},
"id": "GBPj1TVAITht-Rmq",
"cabin": "COACH",
"bookingCode": "Y",
"bookingCodeCount": 9,
"marriedSegmentGroup": "2",
"leg": [{
"kind": "qpxexpress#legInfo",
"id": "Lp142e8MFu5oJvyK",
"aircraft": "321",
"arrivalTime": "2016-03-20T11:15+02:00",
"departureTime": "2016-03-20T07:20+01:00",
"origin": "MUC",
"destination": "IST",
"originTerminal": "1",
"destinationTerminal": "I",
"duration": 175,
"mileage": 976,
"meal": "Meal"
}],
"connectionDuration": 165
}, {
"kind": "qpxexpress#segmentInfo",
"duration": 680,
"flight": {
"carrier": "TK",
"number": "81"
},
"id": "GMi6-VxjbU+icJPl",
"cabin": "COACH",
"bookingCode": "Y",
"bookingCodeCount": 5,
"marriedSegmentGroup": "3",
"leg": [{
"kind": "qpxexpress#legInfo",
"id": "LBkTIA9o3PxzqKDC",
"aircraft": "330",
"arrivalTime": "2016-03-20T19:20-04:00",
"departureTime": "2016-03-20T14:00+02:00",
"origin": "IST",
"destination": "BOS",
"originTerminal": "I",
"destinationTerminal": "E",
"duration": 680,
"mileage": 4814,
"meal": "Meal",
"secure": true
}]
}]
代碼
因此,一個片段的片段。是一個有腿的數組。要獲取我使用此代碼的航班信息。
<?php
function getInformation($slices) {
$url = "https://www.googleapis.com/qpxExpress/v1/trips/search?key=INSERTYOURAPIKEYHERE";
$postData = '{
"request": {
"passengers": {
"adultCount": 1
},
"slice": ' . json_encode($slices) . '
}
}';
$curlConnection = curl_init();
curl_setopt($curlConnection, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($curlConnection, CURLOPT_URL, $url);
curl_setopt($curlConnection, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curlConnection, CURLOPT_POSTFIELDS, $postData);
curl_setopt($curlConnection, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curlConnection, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlConnection, CURLOPT_SSL_VERIFYPEER, FALSE);
//$results = json_decode(curl_exec($curlConnection), true);
if (isset($results['error'])) {
var_dump($results);
exit();
}
// i save the content to a file for better debugging
//return json_decode(file_get_contents('BOSMUC.JSON'), true);
return $results;
}
$slices = array(array('origin' => 'BOS', 'destination' => 'MUC', 'date' => "2016-03-16")
, array('origin' => 'MUC', 'destination' => 'BOS', 'date' => "2016-03-20"));
$resultAsArray = getInformation($slices);
$trips = array_filter($resultAsArray['trips']['tripOption'], function($kind) {
if (!isset($kind['kind'])) {
return false;
}
if ($kind['kind'] == "qpxexpress#tripOption") {
return true;
}
return false;
});
foreach ($trips as $trip) {
echo "------- NEW FLIGHT ---------\n";
echo "FLight Cost: " . $trip['saleTotal'] . "\n";
foreach ($trip['slice'] as $index => $slice) {
print "SLICE $index: " . $slices[$index]['origin'] . " TO " . $slices[$index]['destination'] . "\n";
foreach ($slice['segment'] as $segment) {
foreach ($segment['leg'] as $leg) {
print "FROM " . $leg['origin'] . " to " . $leg['destination'] . " (" . $leg['departureTime'] . "-" . $leg['arrivalTime'] . ")" . "\n";
}
}
}
}
所以這只是一個開始。但應該是一個好的開始。要使代碼運行,您必須將INSERTYOURAPIKEYHERE
與您自己的api密鑰重新分區。
輸出
------- NEW FLIGHT ---------
FLight Cost: USD5988.36
SLICE 0: BOS TO MUC
FROM BOS to MUC (2016-03-16T21:20-04:00-2016-03-17T09:40+01:00)
SLICE 1: MUC TO BOS
FROM MUC to BOS (2016-03-20T15:35+01:00-2016-03-20T19:35-04:00)
------- NEW FLIGHT ---------
FLight Cost: USD6020.66
SLICE 0: BOS TO MUC
FROM BOS to MUC (2016-03-16T21:20-04:00-2016-03-17T09:40+01:00)
SLICE 1: MUC TO BOS
FROM MUC to FRA (2016-03-20T09:00+01:00-2016-03-20T10:10+01:00)
FROM FRA to BOS (2016-03-20T10:55+01:00-2016-03-20T14:20-04:00)
------- NEW FLIGHT ---------
FLight Cost: USD12115.86
SLICE 0: BOS TO MUC
FROM BOS to LHR (2016-03-16T20:30-04:00-2016-03-17T07:20+00:00)
FROM LHR to MUC (2016-03-17T08:55+00:00-2016-03-17T11:50+01:00)
SLICE 1: MUC TO BOS
FROM MUC to LHR (2016-03-20T07:00+01:00-2016-03-20T08:05+00:00)
FROM LHR to BOS (2016-03-20T09:25+00:00-2016-03-20T13:09-04:00)
剛剛過去的3個輸出。