Jan*_*Jan 1 php api json google-api-php-client
我想找到特价航班的最低价格,并希望使用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);
Run Code Online (Sandbox Code Playgroud)
(我已经在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);
Run Code Online (Sandbox Code Playgroud)
结果错误:
<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>
Run Code Online (Sandbox Code Playgroud)
这个错误会引起别人的注意吗?
我在同一条船上.我是一个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;
?>
Run Code Online (Sandbox Code Playgroud)
这就是我现在所处的位置.当然,可以使用PHP设置搜索变量,并且 solutions => "1"可以将其设置为所需的解决方案数量.希望能帮助到你!
| 归档时间: |
|
| 查看次数: |
5900 次 |
| 最近记录: |