1

我正在尝试从 R (httr) 查询 QPX Exprs (Google),但无论出于何种原因,我都得到 0 个结果。这是我的查询:

x <- list(
    request = list(
      slice = list(c(origin = "BOS", destination = "LAX", date = "2014-07-29")), 
      passengers = c(adultCount = 1, infantInLapCount = 0, infantInSeatCount = 0,
                     childCount = 0, seniorCount = 0),
      solutions = 10,
      refundable = "false")
    )

这是命令:

POST("https://www.googleapis.com/qpxExpress/v1/trips/search?key=MY_KEY",
     body = toJSON(x), add_headers("Content-Type" = "application/json"), verbose(),
     add_headers(Expect = ""))

最后,来自谷歌的回应:

* About to connect() to www.googleapis.com port 443 (#0)
*   Trying 173.194.66.95... * connected
* Connected to www.googleapis.com (173.194.66.95) port 443 (#0)
* successfully set certificate verify locations:
*   CAfile: C:/Users/XXXX/Documents/R/win-library/3.1/httr/cacert.pem
  CApath: none
* SSL re-using session ID
* SSL connection using ECDHE-RSA-RC4-SHA
* Server certificate:
*    subject: C=US; ST=California; L=Mountain View; O=Google Inc; CN=*.googleapis.com
*    start date: 2014-07-02 13:35:47 GMT
*    expire date: 2014-09-30 00:00:00 GMT
*    subjectAltName: www.googleapis.com matched
*    issuer: C=US; O=Google Inc; CN=Google Internet Authority G2
*    SSL certificate verify ok.
> POST /qpxExpress/v1/trips/search?key=MY_KEY HTTP/1.1
Host: www.googleapis.com
Accept: */*
Accept-Encoding: gzip
user-agent: curl/7.19.6 Rcurl/1.95.4.1 httr/0.3
Content-Type: application/json
Content-Length: 220

< HTTP/1.1 200 OK
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: Fri, 01 Jan 1990 00:00:00 GMT
< Date: Mon, 21 Jul 2014 10:39:46 GMT
< ETag: "FHaaT3rgbj6tTc1zJmPkVQ6bD-8/wa9h__cUdEwRE2bp0yW5NTA6fec"
< Content-Type: application/json; charset=UTF-8
< Content-Encoding: gzip
< X-Content-Type-Options: nosniff
< X-Frame-Options: SAMEORIGIN
< X-XSS-Protection: 1; mode=block
< Server: GSE
< Alternate-Protocol: 443:quic
< Transfer-Encoding: chunked
< 
* Connection #0 to host www.googleapis.com left intact
Response [https://www.googleapis.com/qpxExpress/v1/trips/search?key=MY_KEY]
  Status: 200
  Content-type: application/json; charset=UTF-8
{
 "kind": "qpxExpress#tripsSearch",
 "trips": {
  "kind": "qpxexpress#tripOptions",
  "requestId": "UTlu4NDcLz3Ypcicp0KKI3",
  "data": {
   "kind": "qpxexpress#data",
   "airport": [
    {
     "kind": "qpxexpress#airportData", ...

有人对此有任何运气吗?

非常感谢!

卡洛斯

4

1 回答 1

2

万一有人感兴趣。以下是 Duncan 和 Hadley 的回答:

Windows 的 postForm 命令如下:

library(RCurl)
library(RJSONIO)

x <- list(
      request = list(
        passengers = list(
          adultCount = 1
        ),
        slice = list(
          list(
            origin = "BOS", 
            destination = "LAX", 
            date = "2014-07-29"
          )
        ), 
        refundable = "false",
        solutions = 10
      )
    )

postForm("https://www.googleapis.com/qpxExpress/v1/trips/search?key=my_KEY",
         .opts = list(postfields = toJSON(x), 
                      cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"),
                      httpheader = c('Content-Type' = 'application/json',
                                     Accept = 'application/json'),
                      verbose = TRUE
                  ))

你可以省略

cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl") 

如果您在 Linux 机器上运行。

如果你更喜欢使用 httr,这里是命令:

library(httr)

url <- "https://www.googleapis.com/qpxExpress/v1/trips/search"
json <- jsonlite::toJSON(x, auto_unbox = TRUE)

POST(url, query = list(key = my_Key), body = json, content_type_json())

干杯。

卡洛斯

于 2014-07-23T15:09:08.003 回答