如何在PHP中显示qpx express

时间:2016-03-08 07:12:40

标签: php google-qpx-express-api

我很难显示我的qpx express API代码。我使用qpx express免费。是qpx表示显示实时航班时刻表吗?我写过像这样的代码

<?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);

是正确的代码吗?我的目标只是在我的网站上显示航班时刻表。有什么想法吗?如果你能帮助我,我会很感激。谢谢

2 个答案:

答案 0 :(得分:0)

您的代码正在运行,但需要请求日期而不是过去。例如,此代码json将找到一些航班。您可以使用this demo flight database检查您的json,而无需向真正的QPX api提交请求:

{
  "request": {
    "passengers": {
      "adultCount": 1
    },
    "slice": [
      {
        "origin": "BOS",
        "destination": "LAX",
        "date": "2016-03-09"
      },
      {
        "origin": "LAX",
        "destination": "BOS",
        "date": "2016-03-09"
      }
    ]
  }
}

答案 1 :(得分:0)

所以我只使用BOS-MUC进行查询。首先要做的是正确的错误报告,所以日期不应该是过去的。我只是阅读tripOptions。对于每个TripOptions,查询中的切片到服务器然后是腿。

一段

作为tripInfos服务器的响应。您的行程分为要求的2个切片。对于每张幻灯片,您都有一个细分。一段看起来像这样。

"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";
            }
        }
    }
}

所以这只是一个开始。但应该是一个良好的开端。要使代码运行,您必须使用自己的api密钥重新INSERTYOURAPIKEYHERE

输出

------- 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个输出。