我正在尝试使用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=xxxxxxxxxxxxxxfrom 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));更多推荐
发布评论