amadeus4dev / amadeus-php
Amadeus 自助服务 API 的 PHP 库
Requires
- php: >=7.4.0
- ext-curl: *
- ext-json: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- phpstan/phpstan: ^1.5.2
- phpunit/phpunit: ^9.5.19
This package is auto-updated.
Last update: 2024-08-30 01:38:21 UTC
README
Amadeus 为旅行行业提供了一套丰富的 API。更多详情,请查看Amadeus 开发者门户。
⚠️ 重要信息 ⚠️
❗ 此 SDK 仅由开发者社区维护。Amadeus 开发者团队不支持或维护它。❗
跳转至
安装
此库需要 PHP 7.4+。您可以通过 Composer 安装 SDK
composer require amadeus4dev/amadeus-php
入门
要调用第一个 API,您需要注册 Amadeus 开发者账户并设置您的第一个应用程序。
<?php declare(strict_types=1); use Amadeus\Amadeus; use Amadeus\Exceptions\ResponseException; require __DIR__ . '/vendor/autoload.php'; // include composer autoloader try { $amadeus = Amadeus::builder("REPLACE_BY_YOUR_API_KEY", "REPLACE_BY_YOUR_API_SECRET") ->build(); // Flight Offers Search GET $flightOffers = $amadeus->getShopping()->getFlightOffers()->get( array( "originLocationCode" => "PAR", "destinationLocationCode" => "MAD", "departureDate" => "2022-12-29", "adults" => 1 ) ); print $flightOffers[0]; // Flight Offers Search POST $body ='{ "originDestinations": [ { "id": "1", "originLocationCode": "PAR", "destinationLocationCode": "MAD", "departureDateTimeRange": { "date": "2022-12-29" } } ], "travelers": [ { "id": "1", "travelerType": "ADULT" } ], "sources": [ "GDS" ] }'; $flightOffers = $amadeus->getShopping()->getFlightOffers()->post($body); print $flightOffers[0]; } catch (ResponseException $e) { print $e; }
初始化
客户端可以直接初始化
//Initialize using parameters $amadeus = Amadeus ::builder("REPLACE_BY_YOUR_API_KEY", "REPLACE_BY_YOUR_API_SECRET") ->build();
或者,如果没有参数,则可以通过环境变量 AMADEUS_CLIENT_ID
和 AMADEUS_CLIENT_SECRET
来初始化。
$amadeus = Amadeus ::builder() ->build();
您的凭据可以在Amadeus 控制台找到。
默认情况下,SDK 设置为 test
环境。要切换到 production
(按使用付费)环境,请按以下方式切换主机名
//Initialize using parameters $amadeus = Amadeus ::builder("REPLACE_BY_YOUR_API_KEY", "REPLACE_BY_YOUR_API_SECRET") ->setProductionEnvironment() ->build();
使用 SSL 证书
此库使用 PHP 核心扩展 cURL 进行 Http 请求,但禁用了 SSL 证书验证选项。因此,强烈建议使用支持 PHP cURL 函数的证书。
您可以从官方 cURL 网站下载 cacert.pem
证书包。一旦您下载了 cacert.pem
文件,您应该将其移动到最适合您和您的设置的目录。
// Set your certificate path for opening SSL verification $amadeus->getClient()->setSslCertificate($REPLACE_BY_YOUR_SSL_CERT_PATH);
文档
Amadeus 拥有一套庞大的 API,我们的文档旨在帮助您入门。有关每个 SDK 方法、其参数和返回类型的详细信息,请参阅我们的参考文档。
调用 API
此库方便地将每个 API 路径映射到类似的路径。
例如,GET /v1/airport/direct-destinations?departureAirportCode=MAD
将是
$amadeus->getAirport()->getDirectDestinations()->get(["departureAirportCode" => "MAD"]);
同样,要按 ID 选择资源,您可以将 ID 传递到 单一 路径中。
例如,GET /v2/shopping/hotel-offers/XXX
将是
$amadeus->getShopping()->getHotelOffer("XXX")->get();
此外,您也可以直接使用以下提供的方法进行任何任意的 API 调用
// Make a GET request only using path $amadeus->getClient()->getWithOnlyPath("/v1/airport/direct-destinations?departureAirportCode=MAD"); // Make a GET request using path and passed parameters $amadeus->getClient()->getWithArrayParams("/v1/airport/direct-destinations", ["departureAirportCode" => "MAD"]); // Make a POST request using path and passed body $amadeus->getClient()->postWithStringBody("/v1/shopping/availability/flight-availabilities", $body);
请注意,这返回一个原始的 Resource
。
响应
每个成功的 API 调用都返回一个 Resource
对象。该 Resource
对象提供了原始响应体(以字符串格式)。
$locations = $amadeus->getReferenceData()->getLocations()->get( array( "subType" => "CITY", "keyword" => "PAR" ) ); // The raw response, as a string $locations[0]->getResponse()->getResult(); // Include response headers $locations[0]->getResponse()->getBody(); //Without response headers // Directly get response headers as an array $locations[0]->getResponse()->getHeadersAsArray(); // Directly get response body as a Json Object $locations[0]->getResponse()->getBodyAsJsonObject(); // Directly get the data part of response body $locations[0]->getResponse()->getData(); // Directly get the dictionary part of the response body $locations[0]->getResponse()->getDictionary();
日志和调试
您可以在初始化期间通过参数或在 AMADEUS_LOG_LEVEL
环境变量中启用默认 HTTP 客户端的调试。
$amadeus = Amadeus::builder() ->setLogLevel("debug") ->build();
支持的端点列表
/* Flight Offers Search GET */ // function get(array $params) : $amadeus->getShopping()->getFlightOffers()->get( array( "originLocationCode" => "PAR", "destinationLocationCode" => "MAD", "departureDate" => "2022-12-29", "adults" => 1 )); /* Flight Offers Search POST */ // function get(string $body) : $amadeus->getShopping()->getFlightOffers()->post($body); /* SeatMap Display */ // function get(string $params) $amadeus->getShopping()->getSeatMaps()->get( array( "flightOrderId" => "eJzTd9f3NjIJdzUGAAp%2fAiY=" ) ) // function post(string $body) $amadeus->getShopping()->getSeatMaps()->post($body) /* Flight Offers Price */ // function post(string $body) : $amadeus->getShopping()->getFlightOffers()->getPricing()->post($body); // function postWithFlightOffers(array $flightOffers, ?array $payments = null, ?array $travelers = null, ?array $params = null) : $flightOffers = $this->getShopping()->getFlightOffers()->get(["originLocationCode"=>"SYD", "destinationLocationCode"=>"BKK", "departureDate"=>"2022-11-01", "adults"=>1, "max"=>6]); $amadeus->getShopping()->getFlightOffers()->getPricing()->postWithFlightOffers($flightOffers); /* Flight Create Orders */ // function post(string $body) : $amadeus->getBooking()->getFlightOrders()->post($body); // function postWithFlightOffersAndTravelers(array $flightOffers, array $travelers); $amadeus->getBooking()->getFlightOrders()->postWithFlightOffersAndTravelers($flightOffers, $travelers); /* Flight Choice Prediction */ // function post(string $body) : $amadeus->getShopping()->getFlightOffers()->getPrediction()->post($body); // function postWithFlightOffers(array $flightOffers) : $flightOffers = $this->getShopping()->getFlightOffers()->get(["originLocationCode"=>"LON", "destinationLocationCode"=>"NYC", "departureDate"=>"2022-12-06", "adults"=>1, "max"=>20]); $amadeus->getShopping()->getFlightOffers()->getPrediction()->postWithFlightOffers($flightOffers); /* Airport and City Search (autocomplete) */ // Get a list of airports and cities matching a given keyword // function get(array $params) : $amadeus->getReferenceData()->getLocations(["subType"=>"CITY,AIRPORT", "keyword"=>"MUC"]); // Get a specific airport or city based on its id // function get() : $amadeus->getReferenceData()->getLocation("CMUC")->get(); /* Hotel Name Autocomplete */ // function get(array $params) : $amadeus->getReferenceData()->getLocations()->getHotel()->get(["keyword"=>"PARI", "subType"=>"HOTEL_GDS"]); /* Hotel List */ // Get list of hotels by hotel id // function get(array $params) : $amadeus->getReferenceData()->getLocations()->getHotels()->getByHotels()->get(["hotelIds"=>"XXX"]); // Get list of hotels by city code // function get(array $params) : $amadeus->getReferenceData()->getLocations()->getHotels()->getByCity()->get(["cityCode"=>"PAR"]); // Get list of hotels by a GeoCode // function get(array $params) : $amadeus->getReferenceData()->getLocations()->getHotels()->getByGeocode()->get(["longitude"=2.160873, "latitude"=>41.397158]); /* Hotel Search */ // Get list of available offers by hotel ids // function get(array $params) : $amadeus->getShopping()->getHotelOffers()->get(["hotelId" => "MCLONGHM","adults" => 1]); // Check conditions of a specific offer // function get() : $amadeus->getShopping()->getHotelOffer("XXX")->get(); /* Hotel Booking */ // The offerId comes from the hotel offer above // function post(string $body) : $amadeus->getBooking()->getHotelBookings()->post($body); /* Hotel Ratings */ // function get(array $params) : $amadeus->getEReputation()->getHotelSentiments()->get(["hotelIds" => "TELONMFS"]); /* Flight Availabilities Search */ // function post(string $body) : $amadeus->getShopping()->getAvailability()->getFlightAvailabilities()->post($body); /* Airport Routes */ // function get(array $params) : $amadeus->getAirport()->getDirectDestinations()->get(["departureAirportCode" => "MAD", "max" => 2]); /* Flight Cheapest Date Search */ // function get(array $params) : $amadeus->getShopping()->getFlightDates()->get(["origin"=>"MAD", "destination"=>"LON"]); /* Airline Code Lookup */ // function get(array $params) : $amadeus->getReferenceData()->getAirlines()->get(["airlineCodes"=>"BA"]); /* On-Demand Flight Status */ // function get(array $params) : $amadeus->getSchedule()->getFlights()->get(["carrierCode"=>"IB", "flightNumber"=>532, "scheduledDepartureDate"=>"2022-09-23"]); /* Airport Nearest Relevant */ // function get(array $params) : $amadeus->getReferenceData()->getLocations()->getAirports()->get(["latitude"=>51.57285, "longitude"=>-0.44161, "radius"=>500]); /* Flight Delay Prediction */ // function get(array $params) : $amadeus->getTravel()->getPredictions()->getFlightDelay()->get([ "originLocationCode"=>"NCE", "destinationLocationCode"=>"ATH", "departureDate"=>"2022-10-06", "departureTime"=>"18:40:00", "arrivalDate"=>"2022-10-06", "arrivalTime"=>"22:05:00", "aircraftCode"=>"32N", "carrierCode"=>"A3", "flightNumber"=>"691", "duration"=>"PT2H25M" ]); /* Travel Restrictions */ // function get(array $params) : $amadeus->getDutyOfCare()->getDiseases()->getCovid19AreaReport()->get(["countryCode"=>"US"]); /* Flight Inspiration Search */ // function get(array $params) : $amadeus->getShopping()->getFlightDestination()->get(["origin"=>"MUC"]); /* Tours and Activities */ // What are the popular activities in Barcelona (based a geo location and a radius) // function get(array $params) : $activities = $amadeus->getShopping()->getActivities()->get( ["longitude" => 2.160873, "latitude" => 41.397158]); // What are the popular activities in Barcelona? (based on a square) // function get(array $params) : $activities = $amadeus->getShopping()->getActivities()->getBySquare()->get( ["west" => 2.160873, "north" => 41.397158, "south" => 41.394582, "east" => 2.177181]); // Get a single activity from a given id // function get() : $amadeus->getShopping()->getActivity("3044851")->get(); /* Travel Recommendations */ // function get(array $params) : $amadeus->getReferenceData()->getRecommendedLocations()->get(["cityCodes"=>"PAR", "travelerCountryCode"=>"FR"]); /* Airport On-Time Performance */ // function get(array $params) : $amadeus->getAirport()->getPredictions()->getOnTime()->get( ["airportCode" => "NCE", "date" => 2022-11-01]);
开发和贡献
想要贡献?请阅读我们的贡献指南,了解在开发环境中安装和运行此代码的指南。
许可协议
本库采用MIT 许可协议发布。
获取帮助
您可以在StackOverflow上找到我们,或加入我们的开发者社区Discord。