我正在努力寻找特价航班的最低价格,并希望使用Google Developers提供的QPX Express API。由于我是PHP开发人员,我认为Google APIs Client Library for PHP是我选择的方法。
然而,在使用QPX Express API组合PHP API时,我感到困惑:
关于QPX Express,我知道我必须创建一个必须发送到API的JSON对象,甚至可以通过https://qpx-express-demo.itasoftware.com/上的演示轻松完成。
关于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),而simple example没有帮助很大,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);
产生的错误:
<b>Fatal error</b>: 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 <b>C:\dev\www\Google\Http\REST.php</b> on line <b>79</b>
这个错误会引起别人的注意吗?
答案 0 :(得分:5)
我在同一条船上。我是一个主要使用PHP的网络开发人员,我试图将QPX Express API集成到一个项目中。我的开始方式和你做的一样,但没有成功。我不完全确定QPX适用于PHP的API库,所以我重新开始,并使用curl来提出这个问题,这是API推荐的:
<?php
$data = array ( "request" => array(
"passengers" => array(
adultCount => 1
),
"slice" => array(
array(
origin => "BOS",
destination => "LAX",
date => "2014-09-09"),
array(
origin => "LAX",
destination => "BOS",
date => "2014-09-10"),
),
solutions => "1"
),
);
$data_string = json_encode($data);
$ch = curl_init('https://www.googleapis.com/qpxExpress/v1/trips/search?key=MyAPIKey');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
/* then I echo the result for testing purposes */
echo $result;
?>
这就是我现在所处的位置。当然,可以使用PHP设置搜索变量,solutions => "1"
可以设置为您需要的解决方案数量。希望它有所帮助!
答案 1 :(得分:3)
我设法让PHP API Client Client for PHP工作。主要问题是文档不清楚搜索需要哪些参数。阅读FAQ部分和DEMO中最简单的JSON请求后,发现:
{
"request": {
"slice": [
{
"origin": "ZZZ",
"destination": "ZZZ",
"date": "YYYY-MM-DD"
}
],
"passengers": {
"adultCount": 1
},
"solutions": 20
}
}
假设你得到这样的服务:
$client = new \Google_Client();
$client->setDeveloperKey($apiKey);
$this->client = $client;
$this->service = new \Google_Service_QPXExpress($this->client);
搜索请求可能是这样的:
$slices = [];
$slice = new \Google_Service_QPXExpress_SliceInput();
$slice->setOrigin($origin);
$slice->setDestination($destination);
$slice->setDate(isset($date)? $date->format('Y-m-d') : '2015-08-13' );
$slices[] = $slice;
$passengers = new \Google_Service_QPXExpress_PassengerCounts();
$passengers->setAdultCount($pax);
$options = new \Google_Service_QPXExpress_TripOptionsRequest();
$options->setSlice($slices);
$options->setPassengers($passengers);
$options->setSolutions(20);
$request = new \Google_Service_QPXExpress_TripsSearchRequest();
$request->setRequest($options);
/** @var \Google_Service_QPXExpress_TripsSearchResponse $result */
$result = $this->service->trips->search($request);
/** @var \Google_Service_QPXExpress_TripOptionsResponse $trips */
$trips = $result->getTrips();
答案 2 :(得分:0)
在我看来,好像你需要继续这样:
$result = $service->$trips->search(new Google_Service_QPXExpress_TripsSearchRequest());
$trips = $result->getTrips();
$data = $trips->getData(); //Google_Service_QPXExpress_Data
$aircraft = $data->getAircraft();
希望这会让你更进一步。干杯!
(您可能需要对搜索进行参数化)