XML with api to wordpress
-
Hello, I have the following problem. I would like to put the traffic information from our national provider on my site. I have already tried various plug-ins, but it didn’t work. I also tried chat GPT, but that doesn’t work either. Would anyone like to help me with it? The script is here, but I can’t get it to appear on the site now
https://cris-admin.anwb.nl/export-api/v1/pqfeed/latest
[private information removed by moderator]Has anyone a solution
Regards
Goos
-
This topic was modified 1 year, 7 months ago by
Steven Stern (sterndata).
The page I need help with: [log in to see the link]
-
This topic was modified 1 year, 7 months ago by
-
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.phpinside your plugins directory. then enable theanwb-traffic-infoplugin.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 1 year, 7 months ago by
tarhe.
-
This reply was modified 1 year, 7 months ago by
tarhe.
-
This reply was modified 1 year, 7 months ago by
tarhe.
-
This reply was modified 1 year, 7 months ago by
tarhe.
-
This reply was modified 1 year, 7 months ago by
Steven Stern (sterndata).
Hi
I am not so good with php was wondering if you could help me out on this
Goos
Hi @meganmante
We’re unable to help further than this but for now the plugin above should work 90% to output the response from the above API when you install the plugin add the shortcode
[anwb_traffic_info]to any page or post where you want the traffic data to appear.You’re welcome to reach out to a developer to help expand the functionality further, just wanted to demonstrate how you can make it work.
Ok i see . Where must i put the php code i am usin Elementor
Goos
You can follow the steps here on the link to add a shortcode on your page/post from Elementor.
I hope that helps 🙂
Yes i understand that but how can i add the php code in to elementor
Please follow the steps below to use the plugin.
- Download the plugin from here – https://file.io/RfKuedub1CqT
- Install and activate the plugin from WordPress admin > plugins
- Add this shortcode
[anwb_traffic_info]on elementor with the steps here to any of your page/post and save your page
Once you follow the above process you see the traffic data displaying on the page/post you added it to.
Hmm recieve this message
The transfer you requested has been deleted
Look like the link got expired. Please download here as soon as you can before it expires again.
i am seeimg this now
Organization: ANWB/NDW/NLRWS_0004371023 TrafficType: 0 Country: NL RoadNumber: A2 SegID: 3104 SegStartName: 's-Hertogenbosch SegEndName: Utrecht FromCarriagewayType: ConnectingCarriageWay ToCarriagewayType: ConnectingCarriageWay MainLocation: A2 's-Hertogenbosch richting Utrecht, TMCPriLoc: 7988 TMCPriName: knp. Oudenrijn naar de A12 richting Arnhem HMPriLoc: 63.6 CoordPriLoc: CoordSecLoc: DetailLoc: bij knp. Oudenrijn naar de A12 richting Arnhem CodeDirection: 1 Stop: 2024-11-06T04:00:00 Events: FullText: A2 's-Hertogenbosch richting Utrecht, bij knp. Oudenrijn naar de A12 richting Arnhem wegwerkzaamheden, verbindingsweg dicht. Verwachte eindtijd: 06 november 2024 05:00How can we get only the text
FullText: A2 ‘s-Hertogenbosch richting Utrecht, bij knp. Oudenrijn naar de A12 richting Arnhem wegwerkzaamheden, verbindingsweg dicht. Verwachte eindtijd: 06 november 2024 05:00`
It is working but now we need to get the text
FullText: information with every report how can we get that
Goos
Good to know you were able to get the plugin to work to modify the plugin to display only the
FullTextfield from each traffic message, you can update theforeachloop to check specifically for that field.
You can download the new modified plugin here that will display theFullTextfield only. You just need to update the previous plugin with this new version to see only theFullTextfield on the response.must i use [FullText]
Yes, since the XML data from the API contains a
<FullText>field that includes the primary information you want to display, then accessing$message->FullTextis the way to go. -
This reply was modified 1 year, 7 months ago by
The topic ‘XML with api to wordpress’ is closed to new replies.