php
  • google-qpx-express-api
  • 2016-03-08 50 views 1 likes 
    1

    我很困惑,顯示我的代碼爲qpx express API。我使用qpx免費。是否顯示實時航班時刻表?我寫過這樣的代碼如何在PHP中顯示qpx express

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

    是不是正確的代碼?我的目標是隻在我的網站上顯示航班時刻表。任何想法?如果你能幫助我,我將不勝感激。謝謝

    回答

    0

    您的代碼正在工作,但需要請求的日期不是過去。例如,這個代碼json會找到一些航班。所以我只是用BOS-MUC我查詢

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

    :您可以使用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個輸出。

    相關問題