Hello @meganmante
To be able to get the content from the above API you’ll need to develop a custom plugin that can query the XML API – https://cris-admin.anwb.nl/export-api/v1/pqfeed/latest and the display the response via shortcode.
This can be typically done by consulting a WordPress developer. To help you get started if it’s something you’d like to do yourself you can work with the sample plugin below as a start point. The plugin is currently working when i tested it, your API key is already added to the plugin code
Save the above code as a PHP file e.g., anwb-traffic-info.php
inside your plugins directory. then enable the anwb-traffic-info
plugin.
Once it’s enabled, add the shortcode [anwb_traffic_info]
to any page or post where you want the traffic data to appear. Please ensure you work further on the plugin to improve the functionality before using it on a production site.
<?php
/*
Plugin Name: ANWB Traffic Info
Description: Displays live traffic information from ANWB API using an API key.
Version: 1.0
Author: Your Name
*/
function fetch_and_display_anwb_traffic_info() {
// Define the cache key and expiration time
$cache_key = 'anwb_traffic_info_cache';
$cache_expiration = HOUR_IN_SECONDS; // Cache duration (1 hour)
// Try to get the cached data
$cached_data = get_transient($cache_key);
if ($cached_data !== false) {
// Return the cached data if it exists
return $cached_data;
}
$api_url = 'https://cris-admin.anwb.nl/export-api/v1/pqfeed/latest';
$api_key = 'xxxxxxxxxxxxxxxxx';
// Set up the request with the API Key in the headers
$response = wp_remote_get($api_url, [
'headers' => [
'x-api-key' => $api_key
]
]);
if (is_wp_error($response)) {
return 'Error retrieving traffic data: ' . $response->get_error_message();
}
$xml_data = wp_remote_retrieve_body($response);
// Check if we have data
if (empty($xml_data)) {
return 'No data received from the API.';
}
// Parse the XML response
$xml = simplexml_load_string($xml_data);
if (!$xml) {
return 'Error parsing traffic data.';
}
// Start output buffer to capture HTML content
ob_start();
echo '<div class="anwb-traffic-info">';
foreach ($xml->Sections->Section->Messages->Message as $message) {
echo '<div class="traffic-message">';
// Dynamically get and display each field
foreach ($message as $key => $value) {
// Skip empty fields
if (!empty($value)) {
echo '<strong>' . esc_html($key) . ':</strong> ' . esc_html($value) . '<br>';
}
}
echo '</div><hr>';
}
echo '</div>';
// Get buffer contents and clean buffer
$output = ob_get_clean();
// Cache the output for future requests
set_transient($cache_key, $output, $cache_expiration);
return $output;
}
// Register a shortcode to display the data on any page/post
add_shortcode('anwb_traffic_info', 'fetch_and_display_anwb_traffic_info');
-
This reply was modified 3 weeks, 2 days ago by tarhe.
-
This reply was modified 3 weeks, 2 days ago by tarhe.
-
This reply was modified 3 weeks, 2 days ago by tarhe.
-
This reply was modified 3 weeks, 2 days ago by tarhe.
-
This reply was modified 3 weeks, 1 day ago by Steven Stern (sterndata).