Laravel中使用amadeus-php调用Amadeus沙箱API返回不完整响应问题
Laravel使用amadeus-php包调用Flight Offers Search接口仅返回部分字段问题
我用Laravel开发基于Amadeus的个人项目API,使用Amadeus沙箱自助API的Postman集合调用Flight Offers Search接口能正常获取完整航班报价信息,但通过amadeus-php包调用时,仅返回lastTicketingDateTime字段,无法得到预期的完整响应。
控制器代码
public function getFlightOffer(Request $request) { $request->validate([ "originLocationCode" => "required|string", "destinationLocationCode" => "required|string", "adults" => "required|alpha_num", "departureDate" => "required|date", ]); $data = [ "originLocationCode" => $request->originLocationCode, "destinationLocationCode" => $request->destinationLocationCode, "departureDate" => $request->departureDate, "adults" => $request->adults, "max" => $request->max, ]; try { $amadeus = Amadeus::builder()->build(); $ssl_path = base_path() . '/certificates/cacert.pem'; $amadeus->getClient()->setSslCertificate($ssl_path); // Flight Offers Search GET $flightOffers = $amadeus->getShopping()->getFlightOffers()->get( $data ); return response(["data" => $flightOffers], 200, ['Content-type' => 'Appliaction/JSON']); } catch (ResponseException $e) { print $e; } }
请求参数
originLocationCode:SYD destinationLocationCode:BYK departureDate:2023-06-05 returnDate:2023-06-19 adults:1 max:5
当前响应数据
{ "data": [ { "lastTicketingDateTime": "2023-06-05" }, { "lastTicketingDateTime": "2023-05-31" }, { "lastTicketingDateTime": "2023-05-31" } ] }
问题排查与修复建议
遗漏关键请求参数:请求参数中包含
returnDate(往返日期),但代码的$data数组未传入该参数,这会导致API仅返回基础元数据而非完整航班报价。需补充该参数:$data = [ "originLocationCode" => $request->originLocationCode, "destinationLocationCode" => $request->destinationLocationCode, "departureDate" => $request->departureDate, "returnDate" => $request->returnDate, // 新增往返日期参数 "adults" => $request->adults, "max" => $request->max, ];SDK响应对象处理不当:
amadeus-php返回的$flightOffers是封装后的响应对象,直接返回会仅序列化部分字段。需调用SDK提供的方法获取完整原始数据,例如:// 获取完整响应结果 $flightOffersData = $flightOffers->getResult(); return response(["data" => $flightOffersData], 200, ['Content-Type' => 'Application/JSON']);响应头拼写错误:代码中
Content-type的值拼写错误(Appliaction/JSON应为Application/JSON),需修正该拼写错误。异常处理优化:当前
print $e无法将错误信息返回给前端,建议改为返回JSON格式错误响应:catch (ResponseException $e) { return response(["error" => $e->getMessage(), "error_code" => $e->getCode()], 400); }
以上问题修正后,应该能获取到与Postman一致的完整航班报价响应,大概率是代码实现问题而非amadeus-php包本身的问题。
内容的提问来源于stack exchange,提问作者Joy Ram Sen Gupta
相关产品推荐
相关产品推荐

