You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Recreation.gov API获取营地警报信息(WordPress场景)

获取Recreation.gov营地警报的RIDB API字段说明

你当前调用的/facilities/{facilityId}端点仅返回营地基础属性(如经度),不包含警报数据。要获取营地的警报信息,需要调用RIDB的/alerts专用端点,并通过facilityId参数过滤目标营地的警报。

具体实现步骤

  1. API端点与参数
    使用端点:https://ridb.recreation.gov/api/v1/alerts?facilityId={你的营地ID}
    需携带相同的apiKey请求头,返回的JSON数据中,警报列表位于RECDATA数组内。

  2. 核心JSON键说明
    每个警报对象包含以下关键字段:

    • AlertTitle:警报标题
    • AlertDescription:警报详细内容
    • AlertType:警报类型(如警告、通知)
    • StartDate/EndDate:警报生效时间段
  3. WordPress代码示例
    以下是获取并展示营地警报的完整函数:

    /*
    Description: Fetches campground alerts from Recreation.gov API.
    */
    function get_campground_alerts($facilityId) {
        $api_key = 'XXXXXXXXXXXXXXX'; // 替换为你的API密钥
        $endpoint = "https://ridb.recreation.gov/api/v1/alerts?facilityId=$facilityId";
    
        $response = wp_remote_get($endpoint, [
            'headers' => ['apiKey' => $api_key],
            'Accept' => 'application/json'
        ]);
    
        if (is_wp_error($response)) {
            return '获取警报数据失败';
        }
    
        $body = json_decode(wp_remote_retrieve_body($response), true);
        if (isset($body['RECDATA']) && !empty($body['RECDATA'])) {
            return $body['RECDATA'];
        } else {
            return '无可用警报';
        }
    }
    

    在页面模板中展示警报:

    <?php
    $campground_id = 232866; // 示例营地ID
    $rg_alerts = get_campground_alerts($campground_id);
    
    if (is_array($rg_alerts)) {
        echo '<div class="campground-alerts">';
        foreach ($rg_alerts as $alert) {
            echo '<div class="alert-item">';
            echo '<h4>' . esc_html($alert['AlertTitle']) . '</h4>';
            echo '<p>' . esc_html($alert['AlertDescription']) . '</p>';
            if (!empty($alert['StartDate'])) {
                echo '<small>生效时间:' . date('Y-m-d', strtotime($alert['StartDate'])) . '</small>';
            }
            echo '</div>';
        }
        echo '</div>';
    } else {
        echo esc_html($rg_alerts);
    }
    ?>
    

补充说明

  • 如果需要同时获取营地基础信息和警报,可以在WordPress中发起两次独立请求,或使用批量请求优化性能(需遵循RIDB API的批量请求规则)。
  • 注意对返回内容进行esc_html转义,避免XSS风险。

内容的提问来源于stack exchange,提问作者moorewebx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 18:43:20