【问题标题】:Use Mashape with R将 Mashape 与 R 一起使用
【发布时间】:2015-05-21 19:01:05
【问题描述】:

我正在尝试使用 Mashape 平台通过 R 访问不同的 API(例如 epguides 或 pipl)。我可以直接使用原始 API,但习惯 Mashape 似乎是一项不错的投资,因为它提供了对许多其他 API 的统一访问。

但是有两个问题:

  1. Mashape 不提供任何 R 教程。我尝试了httr 包来查询Mashape,但直到那里没有成功。 如何用 R 查询 Mashape? ;
  2. 据我测试,Mashape 上的大多数 API 端点似乎没有响应(即使在平台提供的测试页面上也是如此)。 对于每个托管 API,Mashape 真的可靠吗?

【问题讨论】:

    标签: r httr mashape


    【解决方案1】:

    很好的问题 Gratien,

    这里有一段代码 sn-p 可以帮助您入门:

    正在查询天气 API 以获取加利福尼亚州洛杉矶下周的天气预报!

    记得用你自己的改变 X-Mashape-Key :)

    #imports an http R library
    library(httr)
    
    #perform a GET request on the URL, with two headers and store in a resp variable
    resp <- GET("https://george-vustrey-weather.p.mashape.com/api.php?location=Los+Angeles", add_headers("X-Mashape-Key" = "MASHAPE-OWN-KEY","Accept" = "application/json"))
    
    #Prints the headers
    headers(resp)
    
    #Prints the content of the response
    str(content(resp))
    

    希望这会有所帮助!

    【讨论】:

    • 完美!这绝对有帮助。谢谢你,奥利。
    • 不客气,希望我的回答 - 回答你的问题!
    最近更新 更多