piuozorio Posted March 31, 2015 Report Share Posted March 31, 2015 Hello, i'm trying to use google flight search api but dont know how to "decode" the result. The search is ok, i'm using this code with (solutions => "1") i put 1 just for tests, but want more <?php $data = array ( "request" => array( "passengers" => array( adultCount => 1 ), "slice" => array( array( origin => "LIS", destination => "OPO", date => "2015-05-09"), ), solutions => "1" ), ); $data_string = json_encode($data);$ch = curl_init('https://www.googleap...ch?key=MyAPIKey'); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); $result = curl_exec($ch);curl_close($ch); /* then I echo the result for testing purposes */ echo $result; ?> The echo result show this { "kind": "qpxExpress#tripsSearch", "trips": { "kind": "qpxexpress#tripOptions", "requestId": "0wbdK1I8oibR6DGxe0Lr9f", "data": { "kind": "qpxexpress#data", "airport": [ { "kind": "qpxexpress#airportData", "code": "LIS", "city": "LIS", "name": "Lisbon" }, { "kind": "qpxexpress#airportData", "code": "OPO", "city": "OPO", "name": "Porto Francisco Sa Carneiro" } ], "city": [ { "kind": "qpxexpress#cityData", "code": "LIS", "name": "Lisbon" }, { "kind": "qpxexpress#cityData", "code": "OPO", "name": "Porto" } ], "aircraft": [ { "kind": "qpxexpress#aircraftData", "code": "319", "name": "Airbus A319" } ], "tax": [ { "kind": "qpxexpress#taxData", "id": "YP_003", "name": "Portugal Passenger Service Charge" }, { "kind": "qpxexpress#taxData", "id": "PT_001", "name": "Portugal Security Tax" }, { "kind": "qpxexpress#taxData", "id": "WA_001", "name": "Portugal VAT" }, { "kind": "qpxexpress#taxData", "id": "YQ", "name": "TP YQ surcharge" } ], "carrier": [ { "kind": "qpxexpress#carrierData", "code": "TP", "name": "TAP Portugal" } ] }, "tripOption": [ { "kind": "qpxexpress#tripOption", "saleTotal": "EUR62.72", "id": "6QG2bBg243IUGhiWMnGBu0001", "slice": [ { "kind": "qpxexpress#sliceInfo", "duration": 55, "segment": [ { "kind": "qpxexpress#segmentInfo", "duration": 55, "flight": { "carrier": "TP", "number": "1944" }, "id": "GajwbwJfUhmW4VVm", "cabin": "COACH", "bookingCode": "T", "bookingCodeCount": 9, "marriedSegmentGroup": "0", "leg": [ { "kind": "qpxexpress#legInfo", "id": "LZub2a5ZWG9fzNrA", "aircraft": "319", "arrivalTime": "2015-05-10T00:50+01:00", "departureTime": "2015-05-09T23:55+01:00", "origin": "LIS", "destination": "OPO", "originTerminal": "1", "duration": 55, "mileage": 171, "meal": "Refreshments" } ] } ] } ], "pricing": [ { "kind": "qpxexpress#pricingInfo", "fare": [ { "kind": "qpxexpress#fareInfo", "id": "Apo/c6mAuCr7AQaaRm1wQQ2vypSTnyqzYNW5EG6D196qP", "carrier": "TP", "origin": "LIS", "destination": "OPO", "basisCode": "TFLYOWD" } ], "segmentPricing": [ { "kind": "qpxexpress#segmentPricing", "fareId": "Apo/c6mAuCr7AQaaRm1wQQ2vypSTnyqzYNW5EG6D196qP", "segmentId": "GajwbwJfUhmW4VVm", "freeBaggageOption": [ { "kind": "qpxexpress#freeBaggageAllowance", "bagDescriptor": [ { "kind": "qpxexpress#bagDescriptor", "commercialName": "UPTO50LB 23KG AND62LI 158LCM", "count": 1, "description": [ "Up to 50 lb/23 kg", "Up to 62 li/158 lcm" ], "subcode": "0GO" } ], "pieces": 1 } ] } ], "baseFareTotal": "EUR21.00", "saleFareTotal": "EUR21.00", "saleTaxTotal": "EUR41.72", "saleTotal": "EUR62.72", "passengers": { "kind": "qpxexpress#passengerCounts", "adultCount": 1 }, "tax": [ { "kind": "qpxexpress#taxInfo", "id": "WA_001", "chargeType": "GOVERNMENT", "code": "WA", "country": "PT", "salePrice": "EUR1.26" }, { "kind": "qpxexpress#taxInfo", "id": "PT_001", "chargeType": "GOVERNMENT", "code": "PT", "country": "PT", "salePrice": "EUR4.03" }, { "kind": "qpxexpress#taxInfo", "id": "YP_003", "chargeType": "GOVERNMENT", "code": "YP", "country": "PT", "salePrice": "EUR9.93" }, { "kind": "qpxexpress#taxInfo", "id": "YQ", "chargeType": "CARRIER_SURCHARGE", "code": "YQ", "salePrice": "EUR25.00" }, { "kind": "qpxexpress#taxInfo", "id": "WA_001", "chargeType": "GOVERNMENT", "code": "WA", "country": "PT", "salePrice": "EUR1.50" } ], "fareCalculation": "LIS TP OPO 21.00TFLYOWD EUR 21.00 END XT 4.03PT 2.76WA 9.93YP 25.00YQ", "latestTicketingTime": "2015-04-03T17:19-04:00", "ptc": "ADT" } ] } ] } } 1 Now i want tho "decode" and put the result in variables like foreach ........... $aircraft = ..... //code or name $depicao = .... //get iata $arricao = ...... //get iata and so on Thank you for your help Quote Link to comment Share on other sites More sharing options...
Members Vangelis Posted April 1, 2015 Members Report Share Posted April 1, 2015 This is in JSON format and you need to decode it in an array Have a look here http://php.net/manual/en/function.json-decode.php Quote Link to comment Share on other sites More sharing options...
piuozorio Posted April 2, 2015 Author Report Share Posted April 2, 2015 Thank you for your reply and help. I know you point me in the right direction but i dont understand arrays or is dificult for me, big arrays or arrays inside arrays. I tested with some examples in that link and changed for my results but nothing show, allways empty. Do you know how to do and post the right code for me? All i found is the request and nothing about the response in separate variables, all stops in echo $result; or var_dump($result); Sorry my english and thank you Quote Link to comment Share on other sites More sharing options...
aliatwa Posted May 9, 2015 Report Share Posted May 9, 2015 pls piuozorio could you give the full code ?????/ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.