需要帮助使用Google API Client Library for PHP创建QPX Express请求

 BB15107669916 发布于 2023-01-06 11:30

我想找到特价航班的最低价格,并希望使用Google Developers提供的QPX Express API.由于我是PHP开发人员,我认为用于PHP的Google API客户端库是我的首选方法.

但是,在使用QPX Express API组合PHP API时,我感到困惑:

关于QPX Express,我知道我必须创建一个必须发送到API的JSON对象,甚至可以通过https://qpx-express-demo.itasoftware.com/上的Demo轻松完成.

关于PHP API客户端,我想我必须创建一个Client对象和QPX Express Service对象,如下所示:

require_once 'Google/Client.php';
require_once 'Google/Service/QPXExpress.php';
$client = new Google_Client();
$client->setApplicationName("Testing");
$client->setDeveloperKey("MY_APP_KEY");
$service = new Google_Service_QPXExpress($client);

(我已经在Google Developers Console中创建了一个新项目和一个API KEY.)

但后来我不知道该怎么用它来发送JSON请求并收到JSON响应.我要么找不到正确的资源,要么我对RESTful API缺乏了解......不幸的是,我没有找到类似于特殊情况的教程(PHP API和QPX),简单的例子没有多大帮助, QPX Express参考文献也没有.所以,我希望有人能把我放在正确的轨道上...提前致谢!

更新: 在ämbi的第一个答案的帮助下,我想出了以下代码,导致致命错误.

代码:

require_once 'Client.php';
require_once 'Service/QPXExpress.php';
$client = new Google_Client();
$client->setApplicationName("Testing");
$client->setDeveloperKey("[myKey]");
$service = new Google_Service_QPXExpress($client);
$request = new Google_Service_QPXExpress_TripOptionsRequest();
$request->setMaxPrice('EUR200');
$searchRequest = new Google_Service_QPXExpress_TripsSearchRequest();
$searchRequest->setRequest($request);
$result = $service->trips->search($searchRequest);

结果错误:

Fatal error:  Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/qpxExpress/v1/trips/search?key=[myKey]: (500) Backend Error' in C:\dev\www\Google\Http\REST.php:79
Stack trace:
#0 C:\dev\www\Google\Http\REST.php(44): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request))
#1 C:\dev\www\Google\Client.php(499): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request))
#2 C:\dev\www\Google\Service\Resource.php(195): Google_Client->execute(Object(Google_Http_Request))
#3 C:\dev\www\Google\Service\QPXExpress.php(91): Google_Service_Resource->call('search', Array, 'Google_Service_...')
#4 C:\dev\www\fluege.php(13): Google_Service_QPXExpress_Trips_Resource->search(Object(Google_Service_QPXExpress_TripsSearchRequest))
#5 {main}
  thrown in C:\dev\www\Google\Http\REST.php on line 79

这个错误会引起别人的注意吗?

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有