Reputation: 55
I am confused to show my code for qpx express API. I use qpx express free. Is it qpx express for showing real time flight schedule? I had written this code like this
<?php
$url = "https://www.googleapis.com/qpxExpress/v1/trips/search?key=myapikey";
$postData = '{
"request": {
"passengers": {
"adultCount": 1
},
"slice": [
{
"origin": "BOS",
"destination": "LAX",
"date": "2016-03-05"
},
{
"origin": "LAX",
"destination": "BOS",
"date": "2016-03-05"
}
]
}
}';
$curlConnection = curl_init();
curl_setopt($curlConnection, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($curlConnection, CURLOPT_URL, $url);
curl_setopt($curlConnection, CURLOPT_POST, TRUE);
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 = curl_exec($curlConnection);
is that right code? And my goal is only showing flight schedule in my website. Any idea for that? I'll appreciate if you can help me. Thanks
Upvotes: 1
Views: 682
Reputation: 2465
So I just using BOS-MUC for my query. First thing you have to do a right error reporting, so the dates should be not in the past. I just reading the tripOptions. For each TripOptions the slices in the query to the server and then the legs.
As response from the Server for the tripInfos. You have trips which are divided into the 2 slices requested. And for each slide you have a segment. One segment look like this.
"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
}]
}]
So a segment of a slice. Is an array which has legs. To just get the flight info I used this code.
<?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";
}
}
}
}
So this is just a beginning. But should be a good start. To get the code running you have to repace INSERTYOURAPIKEYHERE
with your own api key.
------- 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)
Just the last 3 Outputs.
Upvotes: 0
Reputation: 5534
Your code is working, but need to request date not from the past. For example this code json will find some flights. You can use this demo flight database to check your json without submitting request to real QPX api:
{
"request": {
"passengers": {
"adultCount": 1
},
"slice": [
{
"origin": "BOS",
"destination": "LAX",
"date": "2016-03-09"
},
{
"origin": "LAX",
"destination": "BOS",
"date": "2016-03-09"
}
]
}
}
Upvotes: 0