Search code examples
phperror-handlingamadeus

Exception when calling DefaultApi->flightInspirationSearch: [400] Error connecting to the API


I am trying to get started with amadeus sandbox travel api , and just getting started with api docs

  <?php
require_once(__DIR__ . '/vendor/autoload.php');

$api_instance = new Swagger\Client\Api\DefaultApi();
$apikey = "jJtGVQ********************RONJZq1"; // string | API Key provided for your account, to identify you for API access. Make sure to keep this API key secret.
$origin = "NYC"; // string | <a href=\"https://en.wikipedia.org/wiki/International_Air_Transport_Association_airport_code\">IATA code</a> of the city from which the traveler will depart. See the location and airport interfaces for more information.
$destination = "LAX"; // string | <a href=\"https://en.wikipedia.org/wiki/International_Air_Transport_Association_airport_code\">IATA code</a> of the city to which the traveler is going
$departure_date = "2017-12-16--2017-12-26"; // string | Range of dates between which the traveler could depart. Dates are specified in the <a href=\"https://en.wikipedia.org/wiki/ISO_8601\">ISO 8601</a> yyyy-MM-dd date format. Ranges are inclusive and ranges of months will apply from the start to the end of the month. If just a single date is specified, only that date will be searched. By default, the date range starts today and applies up to 361 in the future is applied. Past dates are generally not supported, future dates should be in the next 361 days, although results start to become sparse after about 6 months in the future, as airlines may still be defining their schedules. The default is to search all future dates available.
$one_way = false; // bool | When set to true, the query will be for a single trip from the origin to the destination. When this parameter is not provided, or is set to false, the query is for a round trip from the origin to the destination and back again.
$duration = "1--15"; // string | The allowed duration or range of durations of the trip, in days. This parameter must not be set if the one-way parameter is set to true.
$direct = false; // bool | Limit the search to results that do not require the passenger to change plane?
$max_price = "100000"; // string | Maximum price of trips to find in the result set, in the currency specified for this origin and destination pair in the cache contents spreadsheet. So, for example, if the origin is NYC, and the max price is 400, this means 400 USD. If the origin is PAR, and the max price is 400, this means 400 EUR. By default, no limit is applied
$aggregation_mode = ""; // string | Specifies the granularity of results to be found. DESTINATION is the default and finds one result per city. COUNTRY finds one result per country, DAY finds on result for every day in the date range, WEEK finds one result for every week in the date range. Note that specifying a small granularity but a large search scope may result in a huge output. For some very large outputs, the API may refuse to provide a result.

try {
    $result = $api_instance->flightInspirationSearch($apikey, $origin, $destination, $departure_date, $one_way, $duration, $direct, $max_price, $aggregation_mode);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception when calling DefaultApi->flightInspirationSearch: ', $e->getMessage(), PHP_EOL;
}
?>

this is the sample code I am using to perform search but it is giving me an error

Exception when calling DefaultApi

flightInspirationSearch: [400] Error connecting to the API (https://api.sandbox.amadeus.com/v1.2/flights/inspiration-search?apikey=jJt*************************JZq1&origin=NYC&destination=LAX&departure_date=2017-12-16--2017-12-26&one-way=0&duration=1--15&direct=0&max_price=100000&aggregation_mode=)

I would really appreciate if some one could tell me why am I getting this error , I am actually using xampp for locally testing on a windows machine with php > 7.0

I think it is because I am missing dependencies but help is needed thanks

.php_cs

│   .travis.yml
│   autoload.php
│   composer.json
│   composer.lock
│   git_push.sh
│   index.php
│   phpunit.xml.dist
│   README.md
│   tree
│   tree.txt
│   
├───docs
│   ├───Api
│   │       DefaultApi.md
│   │       
│   └───Model
│           
│           
├───lib
│   │   
│   │   
│   ├───Api
│   │       │   │       
│   └───Model
│           
├───test
│   ├───Api
│   │   │       
│   └───Model
│           
│           
└───vendor

Solution

  •  <?php
    require_once(__DIR__ . '/vendor/autoload.php');
    
    $api_instance = new Swagger\Client\Api\DefaultApi();
    $apikey = "jJtGVQ********************RONJZq1"; // string | API Key provided for your account, to identify you for API access. Make sure to keep this API key secret.
    $origin = "NYC"; // string | <a href=\"https://en.wikipedia.org/wiki/International_Air_Transport_Association_airport_code\">IATA code</a> of the city from which the traveler will depart. See the location and airport interfaces for more information.
    $destination = "LAX"; // string | <a href=\"https://en.wikipedia.org/wiki/International_Air_Transport_Association_airport_code\">IATA code</a> of the city to which the traveler is going
    $departure_date = "2017-12-16--2017-12-26"; // string | Range of dates between which the traveler could depart. Dates are specified in the <a href=\"https://en.wikipedia.org/wiki/ISO_8601\">ISO 8601</a> yyyy-MM-dd date format. Ranges are inclusive and ranges of months will apply from the start to the end of the month. If just a single date is specified, only that date will be searched. By default, the date range starts today and applies up to 361 in the future is applied. Past dates are generally not supported, future dates should be in the next 361 days, although results start to become sparse after about 6 months in the future, as airlines may still be defining their schedules. The default is to search all future dates available.
    $one_way = false; // bool | When set to true, the query will be for a single trip from the origin to the destination. When this parameter is not provided, or is set to false, the query is for a round trip from the origin to the destination and back again.
    $duration = "1--15"; // string | The allowed duration or range of durations of the trip, in days. This parameter must not be set if the one-way parameter is set to true.
    $direct = false; // bool | Limit the search to results that do not require the passenger to change plane?
    $max_price = "100000"; // string | Maximum price of trips to find in the result set, in the currency specified for this origin and destination pair in the cache contents spreadsheet. So, for example, if the origin is NYC, and the max price is 400, this means 400 USD. If the origin is PAR, and the max price is 400, this means 400 EUR. By default, no limit is applied
    $aggregation_mode = ""; // string | Specifies the granularity of results to be found. DESTINATION is the default and finds one result per city. COUNTRY finds one result per country, DAY finds on result for every day in the date range, WEEK finds one result for every week in the date range. Note that specifying a small granularity but a large search scope may result in a huge output. For some very large outputs, the API may refuse to provide a result.
    
    try {
        $result = $api_instance->flightInspirationSearch($apikey, $origin, $destination, $departure_date, $one_way, $duration, $direct, $max_price, $aggregation_mode);
        print_r($result);
    } catch (Exception $e) {
        echo 'Exception when calling DefaultApi->flightInspirationSearch: ', $e->getMessage(), PHP_EOL;
    }
    ?>
    

    just change the $one_way = false; to $one_way = "false"; basically it was not taking boolean but takes string as a parameter even though the api say boolean , and also make similar changes to $direct