I am using the Recreation.gov api located here: https://ridb.recreation.gov/docs to retrieve specific campground information and display it on my website. I am able to get it working with many of the values but I cannot find a json key (not sure if that is the right way of saying it) to dispaly the campground alerts. For example on this page: https://www.recreation.gov/camping/campgrounds/232866 you can see Alerts at the top with important information. I want to fetch this information using the API and display it on my WordPress website.
Here is an example of what is currently working for me to fetch the “Longitude” feild (I X’d out the API key):
/*
Description: Fetches live updates from Recreation.gov API.
*/
function get_campground_longitude($facilityId) {
$api_key = 'XXXXXXXXXXXXXXX';
$endpoint = "https://ridb.recreation.gov/api/v1/facilities/$facilityId";
$response = wp_remote_get($endpoint, [
'headers' => ['apiKey' => $api_key],
'Accept' => 'application/json'
]);
if (is_wp_error($response)) {
return 'Error retrieving data';
}
$body = json_decode(wp_remote_retrieve_body($response), true);
if (isset($body['FacilityLongitude'])) {
return $body['FacilityLongitude']; // Corrected JSON path
} else {
return 'Longitude not found'; // Adding error handling for missing longitude
}
}
and I display it on my page template like so:
<?php
$campground_id = 232866; // Example ID
$rg_longitude = get_campground_longitude($campground_id);
echo 'Longitude: ' . esc_html($rg_longitude);
?>
The key above is “FacilityLongitude” and I am trying to find what I would use for the alert section. Thanks!