我想找到特价航班的最低价格,并希望使用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
这个错误会引起别人的注意吗?