Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从R查询QPX Express_R_Httr - Fatal编程技术网

从R查询QPX Express

从R查询QPX Express,r,httr,R,Httr,我试图从R(httr)查询QPX Exprss(谷歌),但无论出于什么原因,我都得到了0个结果。这是我的疑问: x <- list( request = list( slice = list(c(origin = "BOS", destination = "LAX", date = "2014-07-29")), passengers = c(adultCount = 1, infantInLapCount = 0, infantInSeatCount =

我试图从R(httr)查询QPX Exprss(谷歌),但无论出于什么原因,我都得到了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")
    )
最后,谷歌的回应是:

* 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", ...
*即将()连接到www.googleapis.com端口443(#0)
*正在尝试173.194.66.95…*有联系的
*已连接到www.googleapis.com(173.194.66.95)端口443(#0)
*已成功设置证书验证位置:
*CAfile:C:/Users/XXXX/Documents/R/win library/3.1/httr/cacert.pem
卡帕斯:没有
*SSL重新使用会话ID
*使用ECDHE-RSA-RC4-SHA的SSL连接
*服务器证书:
*主题:C=美国;ST=加利福尼亚州;L=山景;O=谷歌公司;CN=*.googleapis.com
*开始日期:2014-07-02 13:35:47 GMT
*过期日期:2014-09-30 00:00:00 GMT
*主题名称:www.googleapis.com
*发行人:C=美国;O=谷歌公司;CN=谷歌互联网管理局G2
*SSL证书验证正常。
>POST/qpxExpress/v1/trips/search?key=MY_key HTTP/1.1
主持人:www.googleapis.com
接受:*/*
接受编码:gzip
用户代理:curl/7.19.6 Rcurl/1.95.4.1 httr/0.3
内容类型:application/json
内容长度:220
有人在这件事上运气好吗

非常感谢


卡洛斯,以防有人感兴趣。以下是邓肯和哈德利的答案:

来自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
                  ))
如果您在Linux机器上运行

如果您更愿意使用httr,下面是命令:

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 = ""))
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())
库(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())