【问题标题】:Using API in Bash without curl在没有 curl 的 Bash 中使用 API
【发布时间】:2013-11-25 20:00:16
【问题描述】:

我正在编写一个脚本,该脚本具有使用天气 API 的功能。现在我只设法用curl 解决了这个问题,但我最近注意到我无法访问需要运行脚本的curl。有没有其他方法可以解决这个问题?我的代码是这样的:

weatherLondon()
{
    echo "<h3>Weather in London</h3>"
    (( $# )) || set -- "London,UK"
    echo "Location: ${1//,/, }"
    curl -s "http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=$1" | perl -ne 's/&amp;deg;/°/g;
                                                                                                s/ \(\d+°F\)//g;
                                                                                                /<title>([^<]+)/ && printf "%s: ",$1;
                                                                                                /<fcttext>([^<]+)/ && print $1,"\n"';
}

我在http://www.commandlinefu.com/commands/view/4821/get-the-weather-forecast-for-the-next-24-to-48-for-your-location找到了这段代码

我对 bash 和 API 都很陌生

【问题讨论】:

    标签: bash api


    【解决方案1】:

    您可以使用旧的wget。它应该是几乎每个 Linux 发行版的一部分(除非没有被管理删除)。将您的 curl 语句替换为:

    wget -q -O - "http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=$1"
    

    -O - 将输出设置为标准输出,-q 抑制调试输出,如 curl 的 -s

    【讨论】:

    • 效果很好!无论如何,您也知道如何为 The Guardians API 进行这项工作吗? theguardian.com/open-platform/getting-started
    • 天气脚本运行良好,我想要另一个用于从 API 获取新闻的脚本,例如 The Guardian。
    • 展示一个完全成熟的 API 集成对于这种格式来说太过分了。大多数 API 供应商会支持一些编程语言的客户端库,或者有开源的客户端库。我会搜索这个..
    猜你喜欢
    • 2017-07-08
    • 2018-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    相关资源
    最近更新 更多