I am creating a travel application and trying to implement the QPX express API. I've had some success, but I am looking for some help.
I am trying to parse the arrival and departure time, which is located in trips/tripOption/slice/segment/leg
.
I am trying to parse from the following code: (sorry for being so long)
{
"kind": "qpxExpress#tripsSearch", "trips": {
"kind": "qpxexpress#tripOptions",
"requestId": "zGRd2g7SO2OR3uDEu0Q3Rc",
"data": {
"kind": "qpxexpress#data",
"airport": [
{
"kind": "qpxexpress#airportData",
"code": "ABZ",
"city": "ABZ",
"name": "Aberdeen Dyce"
},
{
"kind": "qpxexpress#airportData",
"code": "LHR",
"city": "LON",
"name": "London Heathrow"
}
],
"city": [
{
"kind": "qpxexpress#cityData",
"code": "ABZ",
"name": "Aberdeen"
},
{
"kind": "qpxexpress#cityData",
"code": "LON",
"name": "London"
}
],
"aircraft": [
{
"kind": "qpxexpress#aircraftData",
"code": "319",
"name": "Airbus A319"
},
{
"kind": "qpxexpress#aircraftData",
"code": "320",
"name": "Airbus A320"
},
{
"kind": "qpxexpress#aircraftData",
"code": "321",
"name": "Airbus A321"
}
],
"tax": [
{
"kind": "qpxexpress#taxData",
"id": "UB_001",
"name": "United Kingdom Passenger Service Charge Departures"
},
{
"kind": "qpxexpress#taxData",
"id": "GB_001",
"name": "United Kingdom Air Passenger Duty Apd"
}
],
"carrier": [
{
"kind": "qpxexpress#carrierData",
"code": "BA",
"name": "British Airways"
}
]
},
"tripOption": [
{
"kind": "qpxexpress#tripOption",
"saleTotal": "GBP185.93",
"id": "GNz9mcDQwhANkULUdCtRhR002",
"slice": [
{
"kind": "qpxexpress#sliceInfo",
"duration": 100,
"segment": [
{
"kind": "qpxexpress#segmentInfo",
"duration": 100,
"flight": {
"carrier": "BA",
"number": "1309"
},
"id": "G-8Do8J4Ed1CCSbw",
"cabin": "COACH",
"bookingCode": "H",
"bookingCodeCount": 2,
"marriedSegmentGroup": "0",
"leg": [
{
"kind": "qpxexpress#legInfo",
"id": "LeISp98KIk6mBaM9",
"aircraft": "320",
"arrivalTime": "2017-02-25T16:15+00:00",
"departureTime": "2017-02-25T14:35+00:00",
"origin": "ABZ",
"destination": "LHR",
"destinationTerminal": "5",
"duration": 100,
"mileage": 401,
"meal": "Food and Beverages for Purchase"
}
]
}
]
}
],
"pricing": [
{
"kind": "qpxexpress#pricingInfo",
"fare": [
{
"kind": "qpxexpress#fareInfo",
"id": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
"carrier": "BA",
"origin": "ABZ",
"destination": "LON",
"basisCode": "HV2RO"
}
],
"segmentPricing": [
{
"kind": "qpxexpress#segmentPricing",
"fareId": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
"segmentId": "G-8Do8J4Ed1CCSbw"
}
],
"baseFareTotal": "GBP161.00",
"saleFareTotal": "GBP161.00",
"saleTaxTotal": "GBP24.93",
"saleTotal": "GBP185.93",
"passengers": {
"kind": "qpxexpress#passengerCounts",
"adultCount": 1
},
"tax": [
{
"kind": "qpxexpress#taxInfo",
"id": "UB_001",
"chargeType": "GOVERNMENT",
"code": "UB",
"country": "GB",
"salePrice": "GBP11.93"
},
{
"kind": "qpxexpress#taxInfo",
"id": "GB_001",
"chargeType": "GOVERNMENT",
"code": "GB",
"country": "GB",
"salePrice": "GBP13.00"
}
],
"fareCalculation": "ABZ BA LON 161.00HV2RO GBP 161.00 END XT 13.00GB 11.93UB",
"latestTicketingTime": "2017-02-25T09:34-05:00",
"ptc": "ADT"
}
]
},
{
"kind": "qpxexpress#tripOption",
"saleTotal": "GBP185.93",
"id": "GNz9mcDQwhANkULUdCtRhR005",
"slice": [
{
"kind": "qpxexpress#sliceInfo",
"duration": 105,
"segment": [
{
"kind": "qpxexpress#segmentInfo",
"duration": 105,
"flight": {
"carrier": "BA",
"number": "1303"
},
"id": "G8zQDGLwjfHKE25K",
"cabin": "COACH",
"bookingCode": "H",
"bookingCodeCount": 2,
"marriedSegmentGroup": "0",
"leg": [
{
"kind": "qpxexpress#legInfo",
"id": "LWDGrQuSyJsv4smo",
"aircraft": "319",
"arrivalTime": "2017-02-25T09:30+00:00",
"departureTime": "2017-02-25T07:45+00:00",
"origin": "ABZ",
"destination": "LHR",
"destinationTerminal": "5",
"duration": 105,
"mileage": 401,
"meal": "Food and Beverages for Purchase"
}
]
}
]
}
],
"pricing": [
{
"kind": "qpxexpress#pricingInfo",
"fare": [
{
"kind": "qpxexpress#fareInfo",
"id": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
"carrier": "BA",
"origin": "ABZ",
"destination": "LON",
"basisCode": "HV2RO"
}
],
"segmentPricing": [
{
"kind": "qpxexpress#segmentPricing",
"fareId": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
"segmentId": "G8zQDGLwjfHKE25K"
}
],
"baseFareTotal": "GBP161.00",
"saleFareTotal": "GBP161.00",
"saleTaxTotal": "GBP24.93",
"saleTotal": "GBP185.93",
"passengers": {
"kind": "qpxexpress#passengerCounts",
"adultCount": 1
},
"tax": [
{
"kind": "qpxexpress#taxInfo",
"id": "UB_001",
"chargeType": "GOVERNMENT",
"code": "UB",
"country": "GB",
"salePrice": "GBP11.93"
},
{
"kind": "qpxexpress#taxInfo",
"id": "GB_001",
"chargeType": "GOVERNMENT",
"code": "GB",
"country": "GB",
"salePrice": "GBP13.00"
}
],
"fareCalculation": "ABZ BA LON 161.00HV2RO GBP 161.00 END XT 13.00GB 11.93UB",
"latestTicketingTime": "2017-02-25T02:44-05:00",
"ptc": "ADT"
}
]
},
{
"kind": "qpxexpress#tripOption",
"saleTotal": "GBP185.93",
"id": "GNz9mcDQwhANkULUdCtRhR004",
"slice": [
{
"kind": "qpxexpress#sliceInfo",
"duration": 100,
"segment": [
{
"kind": "qpxexpress#segmentInfo",
"duration": 100,
"flight": {
"carrier": "BA",
"number": "1311"
},
"id": "GrrTYvxj3OBwLUle",
"cabin": "COACH",
"bookingCode": "H",
"bookingCodeCount": 1,
"marriedSegmentGroup": "0",
"leg": [
{
"kind": "qpxexpress#legInfo",
"id": "LLa2rlu3AEMaQJ-7",
"aircraft": "320",
"arrivalTime": "2017-02-25T14:00+00:00",
"departureTime": "2017-02-25T12:20+00:00",
"origin": "ABZ",
"destination": "LHR",
"destinationTerminal": "5",
"duration": 100,
"mileage": 401,
"meal": "Food and Beverages for Purchase"
}
]
}
]
}
],
"pricing": [
{
"kind": "qpxexpress#pricingInfo",
"fare": [
{
"kind": "qpxexpress#fareInfo",
"id": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
"carrier": "BA",
"origin": "ABZ",
"destination": "LON",
"basisCode": "HV2RO"
}
],
"segmentPricing": [
{
"kind": "qpxexpress#segmentPricing",
"fareId": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
"segmentId": "GrrTYvxj3OBwLUle"
}
],
"baseFareTotal": "GBP161.00",
"saleFareTotal": "GBP161.00",
"saleTaxTotal": "GBP24.93",
"saleTotal": "GBP185.93",
"passengers": {
"kind": "qpxexpress#passengerCounts",
"adultCount": 1
},
"tax": [
{
"kind": "qpxexpress#taxInfo",
"id": "UB_001",
"chargeType": "GOVERNMENT",
"code": "UB",
"country": "GB",
"salePrice": "GBP11.93"
},
{
"kind": "qpxexpress#taxInfo",
"id": "GB_001",
"chargeType": "GOVERNMENT",
"code": "GB",
"country": "GB",
"salePrice": "GBP13.00"
}
],
"fareCalculation": "ABZ BA LON 161.00HV2RO GBP 161.00 END XT 13.00GB 11.93UB",
"latestTicketingTime": "2017-02-25T07:19-05:00",
"ptc": "ADT"
}
]
},
{
"kind": "qpxexpress#tripOption",
"saleTotal": "GBP185.93",
"id": "GNz9mcDQwhANkULUdCtRhR003",
"slice": [
{
"kind": "qpxexpress#sliceInfo",
"duration": 100,
"segment": [
{
"kind": "qpxexpress#segmentInfo",
"duration": 100,
"flight": {
"carrier": "BA",
"number": "1313"
},
"id": "G9i8bJ8e7SyQdDse",
"cabin": "COACH",
"bookingCode": "H",
"bookingCodeCount": 2,
"marriedSegmentGroup": "0",
"leg": [
{
"kind": "qpxexpress#legInfo",
"id": "L+53r7MThymrOjmD",
"aircraft": "321",
"arrivalTime": "2017-02-25T18:50+00:00",
"departureTime": "2017-02-25T17:10+00:00",
"origin": "ABZ",
"destination": "LHR",
"destinationTerminal": "5",
"duration": 100,
"mileage": 401,
"meal": "Food and Beverages for Purchase"
}
]
}
]
}
],
"pricing": [
{
"kind": "qpxexpress#pricingInfo",
"fare": [
{
"kind": "qpxexpress#fareInfo",
"id": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
"carrier": "BA",
"origin": "ABZ",
"destination": "LON",
"basisCode": "HV2RO"
}
],
"segmentPricing": [
{
"kind": "qpxexpress#segmentPricing",
"fareId": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
"segmentId": "G9i8bJ8e7SyQdDse"
}
],
"baseFareTotal": "GBP161.00",
"saleFareTotal": "GBP161.00",
"saleTaxTotal": "GBP24.93",
"saleTotal": "GBP185.93",
"passengers": {
"kind": "qpxexpress#passengerCounts",
"adultCount": 1
},
"tax": [
{
"kind": "qpxexpress#taxInfo",
"id": "UB_001",
"chargeType": "GOVERNMENT",
"code": "UB",
"country": "GB",
"salePrice": "GBP11.93"
},
{
"kind": "qpxexpress#taxInfo",
"id": "GB_001",
"chargeType": "GOVERNMENT",
"code": "GB",
"country": "GB",
"salePrice": "GBP13.00"
}
],
"fareCalculation": "ABZ BA LON 161.00HV2RO GBP 161.00 END XT 13.00GB 11.93UB",
"latestTicketingTime": "2017-02-25T12:09-05:00",
"ptc": "ADT"
}
]
},
{
"kind": "qpxexpress#tripOption",
"saleTotal": "GBP185.93",
"id": "GNz9mcDQwhANkULUdCtRhR001",
"slice": [
{
"kind": "qpxexpress#sliceInfo",
"duration": 95,
"segment": [
{
"kind": "qpxexpress#segmentInfo",
"duration": 95,
"flight": {
"carrier": "BA",
"number": "1307"
},
"id": "GQZ+5WcC5Xyv4NSo",
"cabin": "COACH",
"bookingCode": "H",
"bookingCodeCount": 1,
"marriedSegmentGroup": "0",
"leg": [
{
"kind": "qpxexpress#legInfo",
"id": "L4AarejVfUroJVY-",
"aircraft": "319",
"arrivalTime": "2017-02-25T12:10+00:00",
"departureTime": "2017-02-25T10:35+00:00",
"origin": "ABZ",
"destination": "LHR",
"destinationTerminal": "5",
"duration": 95,
"mileage": 401,
"meal": "Food and Beverages for Purchase"
}
]
}
]
}
],
"pricing": [
{
"kind": "qpxexpress#pricingInfo",
"fare": [
{
"kind": "qpxexpress#fareInfo",
"id": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
"carrier": "BA",
"origin": "ABZ",
"destination": "LON",
"basisCode": "HV2RO"
}
],
"segmentPricing": [
{
"kind": "qpxexpress#segmentPricing",
"fareId": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
"segmentId": "GQZ+5WcC5Xyv4NSo"
}
],
"baseFareTotal": "GBP161.00",
"saleFareTotal": "GBP161.00",
"saleTaxTotal": "GBP24.93",
"saleTotal": "GBP185.93",
"passengers": {
"kind": "qpxexpress#passengerCounts",
"adultCount": 1
},
"tax": [
{
"kind": "qpxexpress#taxInfo",
"id": "UB_001",
"chargeType": "GOVERNMENT",
"code": "UB",
"country": "GB",
"salePrice": "GBP11.93"
},
{
"kind": "qpxexpress#taxInfo",
"id": "GB_001",
"chargeType": "GOVERNMENT",
"code": "GB",
"country": "GB",
"salePrice": "GBP13.00"
}
],
"fareCalculation": "ABZ BA LON 161.00HV2RO GBP 161.00 END XT 13.00GB 11.93UB",
"latestTicketingTime": "2017-02-25T05:34-05:00",
"ptc": "ADT"
}
]
}
]
}
}
I have two foreach loops working to access the airports and prices:
foreach($json['trips']['data']['airport'] as $item) {
echo "<div>". $item['name'] . "</div>";
}
foreach($json['trips']['tripOption'] as $item) {
echo "<div>". $item['saleTotal'] . "</div>";
}
The following code does not access the arrival time and I'm unsure why!
foreach($json['trips']['tripOption']['slice']['segment']['leg'] as $item) {
echo "<div>". $item['arrivalTime'] . "</div>";
}
I have also used the code below to parse my JSON response.
$json = json_decode($results, true);