代码之家  ›  专栏  ›  技术社区  ›  Carlos

从R查询QPX Express

  •  1
  • Carlos  · 技术社区  · 11 年前

    我试图从R(httr)查询QPX Exprss(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", ...
    

    有人在这方面运气好吗?

    非常感谢!

    卡洛斯

    1 回复  |  直到 11 年前
        1
  •  2
  •   Carlos    11 年前

    万一有人感兴趣。以下是邓肯和哈德利的回答:

    来自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())
    

    干杯

    卡洛斯