php - 谷歌 QPX Express API PHP

标签 php json api

我正尝试在我的网站上使用 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

来 self 的 php 文件。我应该如何管理 json 文件。我想我应该制作一个 php 文件并设置 header 类型,对吗?

我在任何地方都找不到任何东西

最佳答案

您无需为每个请求创建和保存实际的 JSON 文件。您可以简单地创建一个 JSON 字符串并将其作为 POST 负载发送。就执行 curl 而言,您应该会在 PHP Manual 中看到可用的 native 函数。 .具体来说,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));

关于php - 谷歌 QPX Express API PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34725067/

相关文章:

javascript - 无法使用带有下划线模板的 json 对象 ¿ 如何正确修复/格式化它?

php - php 应用程序中的关键代码部分?

php - 尝试从mysql下载两个日期之间的excel

php - 错误未定义索引将图像上传到服务器

javascript - 将 HTML 表格转换为 Json 文件

c# - "Preferred"在 Windows Vista/7 中从 C# 访问网络摄像头的方法

json - 错误 403(禁止):Your client does not have permission to get URL

php - “方法 [validateString] 不存在。”在 laravel 验证

java - Android java循环遍历对象列表

json - 如何使用 Alamofire 发布嵌套的 JSON?