Php 需要帮助使用Google API客户端库创建QPX Express请求吗
我正在努力寻找特殊航班的最低价格,并希望使用谷歌开发者提供的。因为我是一名PHP开发人员,所以我认为PHP是我的首选方法 但是,当涉及到将PHP API与QPX Express API相结合时,我会陷入困境: 关于QPX Express,我知道我必须创建一个JSON对象,该对象必须发送到API,这甚至可以通过上的演示轻松完成 关于PHP API客户端,我想我必须创建一个客户端对象和QPX Express服务对象,如下所示:Php 需要帮助使用Google API客户端库创建QPX Express请求吗,php,json,api,google-api-php-client,Php,Json,Api,Google Api Php Client,我正在努力寻找特殊航班的最低价格,并希望使用谷歌开发者提供的。因为我是一名PHP开发人员,所以我认为PHP是我的首选方法 但是,当涉及到将PHP API与QPX Express API相结合时,我会陷入困境: 关于QPX Express,我知道我必须创建一个JSON对象,该对象必须发送到API,这甚至可以通过上的演示轻松完成 关于PHP API客户端,我想我必须创建一个客户端对象和QPX Express服务对象,如下所示: require_once 'Google/Client.php'; re
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开发者控制台中创建了一个新项目和一个API密钥。)
但是我不知道如何使用它来发送JSON请求和接收JSON响应。我要么没有找到正确的资源,要么我缺乏关于RESTful API的知识。。。不幸的是,我没有找到类似于这种特殊情况(PHPAPI和QPX)的教程,并且没有太多帮助,QPXExpress引用也没有太多帮助。所以,我希望有人能让我走上正轨。。。提前谢谢
更新:
在第一个答案的帮助下,我产生了以下代码,这导致了一个致命错误
守则:
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>
致命错误:未捕获异常“Google\u服务\u异常”,并显示消息“调用POST时出错”https://www.googleapis.com/qpxExpress/v1/trips/search?key=[myKey]:(500)C:\dev\www\Google\Http\REST.php中的“后端错误”:79
堆栈跟踪:
#0 C:\dev\www\Google\Http\REST.php(44):Google\u Http\u REST::decodeHttpResponse(对象(Google\u Http\u请求))
#1C:\dev\www\Google\Client.php(499):Google\u Http\u REST::execute(对象(Google\u客户端),对象(Google\u Http\u请求))
#2c:\dev\www\Google\Service\Resource.php(195):Google\u客户端执行(对象(Google\u Http\u请求))
#3 C:\dev\www\Google\Service\QPXExpress.php(91):谷歌服务资源调用('search',Array',谷歌服务…'))
#4 C:\dev\www\fluege.php(13):Google_服务_QPXExpress_Trips_资源搜索(对象(Google_服务_QPXExpress_TripsSearchRequest))
#5{main}
在第79行的C:\dev\www\Google\Http\REST.php中抛出
这个错误引起了任何人的注意吗?在我看来,好像你需要继续这样做:
$result = $service->$trips->search(new Google_Service_QPXExpress_TripsSearchRequest());
$trips = $result->getTrips();
$data = $trips->getData(); //Google_Service_QPXExpress_Data
$aircraft = $data->getAircraft();
希望这能让你走得更远。干杯
(您可能需要对搜索进行参数化)在我看来,您似乎需要继续这样做:
$result = $service->$trips->search(new Google_Service_QPXExpress_TripsSearchRequest());
$trips = $result->getTrips();
$data = $trips->getData(); //Google_Service_QPXExpress_Data
$aircraft = $data->getAircraft();
希望这能让你走得更远。干杯
(您可能需要将搜索参数化)我也在同一条船上。我是一名web开发人员,最近主要在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”
可以设置为所需的解决方案数量。希望有帮助 我也在同一条船上。我是一名web开发人员,最近主要在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”
可以设置为所需的解决方案数量。希望有帮助 我成功地使用于PHP的Google API客户端库正常工作。主要问题是文档不够清楚,搜索需要哪些参数。阅读第节和中最简单的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();
搜索请求可以如下所示:
$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();
我设法使用于PHP的GoogleAPI客户端库工作。主要问题是文档不够清楚,搜索需要哪些参数。阅读第节和中最简单的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();
搜索请求可以如下所示:
$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();
这看起来很好,非常感谢您的分享!由于这个问题不经常被打开,我也考虑过使用cURL而不是phpapi。我已经复制了你的代码并运行了它,但我只得到了一个
bool(false)
结果。我假设我的API密钥创建不正确,或者cURL在本地机器上无法正常工作。。。编辑:代码在实时服务器上就像一个符咒一样工作。:-)很高兴我能帮忙。我花了一段时间才弄明白。我将稍微编辑一下代码,将结果限制为“x”个解。如果您计划使用此代码,请选择“接受”此答案。我是这个论坛的新手,所以我的声誉需要它能得到的一切帮助!除了“solutions”
定义之外,我还定义了“maxPrice”参数,并定义了我在响应中需要的字段。响应字段可以直接添加到URL。在我的情况下,我想要航班数据和价格:https://www.googleapis.com/qpxExpress/v1/trips/search?key=MY_API_KEY&fields=trips(tripOption(saleTotal,slice(段)(航段(到达时间,出发时间,起点,终点,起点,终点,持续时间,操作披露‘‘)’))
(了解了上的字段)@travallia\u-jeff,谢谢你让我开心。这是一个很好的解决方案。我会绕着它玩,然后跳迪斯科