Google QPX Express API PHP(Google QPX Express API PHP)

系统教程 行业动态 更新时间:2024-06-14 16:57:18
Google QPX Express API PHP(Google QPX Express API PHP)

我正在尝试使用QPX Express API在我的网站上搜索航班。

https://developers.google.com/qpx-express/v1/requests#Examples

我不知道怎么跑

curl -d @request.json --header "Content-Type: application/json" https://www.googleapis.com/qpxExpress/v1/trips/search?key=xxxxxxxxxxxxxx

从我的PHP文件。 我应该如何管理json文件。 我问我应该制作一个php文件并设置标题类型,我是否正确?

我无法从任何地方找到任何东西

I'm trying to use QPX Express API for my website to search for flights.

https://developers.google.com/qpx-express/v1/requests#Examples

I have no idea how to run

curl -d @request.json --header "Content-Type: application/json" https://www.googleapis.com/qpxExpress/v1/trips/search?key=xxxxxxxxxxxxxx

from my php file. And how should I manage the json file. I quess I should make a php file and set the header type, am I correct?

I could not find anything from anywhere

最满意答案

您不需要为每个请求创建和保存实际的JSON文件。 您只需创建一个JSON字符串并将其作为POST有效内容发送。 至于执行curl,您应该看到PHP手册中提供的本机函数。 具体来说, curl_init() , curl_setopt()和curl_exec() 。 这是一个例子......

$url = "https://www.googleapis.com/qpxExpress/v1/trips/search?key=YOUR_API_KEY_HERE"; $postData = '{ "request": { "passengers": { "adultCount": 1 }, "slice": [ { "origin": "BOS", "destination": "LAX", "date": "2016-05-10" }, { "origin": "LAX", "destination": "BOS", "date": "2016-05-15" } ] } }'; $curlConnection = curl_init(); curl_setopt($curlConnection, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); curl_setopt($curlConnection, CURLOPT_URL, $url); curl_setopt($curlConnection, CURLOPT_POST, TRUE); curl_setopt($curlConnection, CURLOPT_POSTFIELDS, $postData); curl_setopt($curlConnection, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($curlConnection, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curlConnection, CURLOPT_SSL_VERIFYPEER, FALSE); $results = curl_exec($curlConnection);

您还可以使用数组创建有效内容,然后使用json_encode()将其转换为JSON字符串。

$postData = array( "request" => array( "passengers" => array( "adultCount" => 1 ), "slice" => array( array( "origin" => "BOS", "destination" => "LAX", "date" => "2016-05-10" ), array( "origin" => "LAX", "destination" => "BOS", "date" => "2016-05-15" ) ) ) );

然后使用

curl_setopt($curlConnection, CURLOPT_POSTFIELDS, json_encode($postData));

You do not need to create and save an actual JSON file for each request. You can simply create a JSON string and send that as the POST payload. As far as executing the curl, you should see the native functions available in the PHP Manual. Specifically, curl_init(), curl_setopt(), and curl_exec(). Here's an example...

$url = "https://www.googleapis.com/qpxExpress/v1/trips/search?key=YOUR_API_KEY_HERE"; $postData = '{ "request": { "passengers": { "adultCount": 1 }, "slice": [ { "origin": "BOS", "destination": "LAX", "date": "2016-05-10" }, { "origin": "LAX", "destination": "BOS", "date": "2016-05-15" } ] } }'; $curlConnection = curl_init(); curl_setopt($curlConnection, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); curl_setopt($curlConnection, CURLOPT_URL, $url); curl_setopt($curlConnection, CURLOPT_POST, TRUE); curl_setopt($curlConnection, CURLOPT_POSTFIELDS, $postData); curl_setopt($curlConnection, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($curlConnection, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curlConnection, CURLOPT_SSL_VERIFYPEER, FALSE); $results = curl_exec($curlConnection);

You could also use an array to create the payload, and then use json_encode() to convert it into a JSON string.

$postData = array( "request" => array( "passengers" => array( "adultCount" => 1 ), "slice" => array( array( "origin" => "BOS", "destination" => "LAX", "date" => "2016-05-10" ), array( "origin" => "LAX", "destination" => "BOS", "date" => "2016-05-15" ) ) ) );

And then use

curl_setopt($curlConnection, CURLOPT_POSTFIELDS, json_encode($postData));

更多推荐

本文发布于:2023-04-12 20:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/646a91034ef9a3f6bc5f8fa2f4e2beb6.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:QPX   Google   Express   PHP   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!