【问题标题】:Accessing Shoothill API in R/ R Studio在 R/R Studio 中访问 Shoothill API
【发布时间】:2014-04-20 23:30:43
【问题描述】:

您好,我正在尝试使用 R studio 访问 R 中的 Floodhill Flood Alert API。 http://www.shoothill.com/floodapi/

我不完全确定如何使用我拥有的 API 密钥登录然后调用 API。

我已成功使用不同的 API 调用 API,例如

library(jsonlite)
jsondata <- fromJSON("http://api.wunderground.com/api/c86b0e891d592775/geolookup/conditions/q/IA/Cedar_Rapids.json")#access api
names(jsondata)
summary(jsondata)

非常感谢有关访问 Shoothill Flood Alert API 的帮助!

【问题讨论】:

  • 获取 API 密钥需要一段时间。在此之前,请参阅下面的非答案答案以获得一些指导。
  • 谢谢 - 如果您能提供帮助,我们将不胜感激。我发现我掌握了 RCurl 的原理,但实现起来有点棘手。
  • 5 分钟前得到 API 密钥的确认。将在上午(嗯,缅因州的上午)看看。
  • 感谢您迄今为止的更新和帮助。

标签: json r api rstudio


【解决方案1】:

从技术上讲,这不是完整的 R 答案,但他们在 API 提供程序页面上给出的示例在 R 中使用 RCurl 包是 100% 可行的:

<?php
error_reporting(E_ALL); 
ini_set('display_errors', 1);

$apiKey = '<Your API Key>';
$url = 'https://apifa.shoothill.com/Account/APILogin/';

$postinfo = "apikey=".$apiKey."&persist=false";

$cookie_file_path = dirname(__FILE__) . "cookie.txt";

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);

curl_setopt($ch, CURLOPT_COOKIE, "cookiename=1");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);

$out = curl_exec($ch);

curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_URL, "https://apifa.shoothill.com/API/Floods");
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: application/json'));

$html = curl_exec($ch);
curl_close($ch);

echo $html;
return $html;
?>

OmegaHat 有一个很详细的explanationRCurl 的使用方法,看完之后你应该可以很好的翻译上面的内容了。

【讨论】:

  • 你永远不应该将CURLOPT_SSL_VERIFYHOST 设置为 0。这是一个巨大的安全漏洞。
  • 这是他们网站上的剪切和粘贴
猜你喜欢
  • 2021-07-26
  • 2022-10-07
  • 2018-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-03
相关资源
最近更新 更多