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
-
What i want is is a bit like this want is a little bit like this site
https://www.omroepwest.nl/verkeer
Only the national roads and the traffic information
how must i handle to get the information to see
$message->FullText
[anwb_traffic_info $message->FullText]
Is this the correct way
to understand it better it is now that I then
must use
[anwb_traffic_info $message->FullText]I see it is working now ,
How can I make sure there is a space in between?
and the road information in a separate columnHow can I make sure there is a space in between?
and the road information in a separate columnand how can we set it like this site it has
https://www.omroepwest.nl/verkeer
with the roadsigbs for the traffic message
A15 Europoort richting Rotterdam
Hectometerpaal 50.6How can we set this up
You can add styling and like CSS and HTML to make it look the way you want as i’ve done below. When you have the time you can also hire a developer to style the plugin to meet your site specification. You can download the modification which i added the space here.
<?php
/*
Plugin Name: ANWB Traffic Info
Description: Displays live traffic information from ANWB API using an API key.
Version: 1.2
Author: Goos
*/
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 = 'ADD-YOUR-API-KEY-HERE';
// 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) {
// Check if FullText field exists and is not empty
if (!empty($message->FullText)) {
echo '<div class="traffic-message">';
// Display the road sign (road name) in a separate box
if (!empty($message->Road)) {
echo '<div class="road-sign">' . esc_html($message->Road) . '</div>';
}
// Display the main location and direction in a separate row
if (!empty($message->MainLocation)) {
echo '<div class="location-direction">' . esc_html($message->MainLocation);
if (!empty($message->From)) {
echo ' richting ' . esc_html($message->From);
}
echo '</div>';
}
// Display hectometer detail if available
if (!empty($message->Hectometer)) {
echo '<div class="hectometer">Hectometerpaal: ' . esc_html($message->Hectometer) . '</div>';
}
// Display the full traffic message text
echo '<div class="message-content">' . esc_html($message->FullText) . '</div>';
echo '</div><hr>';
}
}
echo '</div>'; // Close main container
// 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');Hi will let you know today thanks for letting me know
Goos
how can we reset the update time to 2 min .
And one more question where do i have to put the css script in to elementor
How can I add more space, for example keep 1 line open until the next message? So that there is a little more space in between
How can we reset the update time to 2 min.
To reset the cache update time to 2 minutes, you need to adjust the cache expiration value in the code. Change the
cache_expirationvariable to useMINUTE_IN_SECONDS * 2, which will set it to 2 minutes.And one more question where do i have to put the css script in to elementor
You don’t need to put any CSS into Elementor. All CSS needs to go inside the php code or added separately inside the plugin files which is usually the best practice then you can enqueue the css file from the php code.
To make it easier for you I’ve update the plugin again which you can download here to show this structure as it should be and also update the time to 2 min. please work to modify the plugin to meet any specification you’d like. The styling should look nice at least for now until you update it further to meet your needs.
-
This reply was modified 1 year, 7 months ago by
tarhe.
Hmm upload the file but i am seeing this
‘; } echo ‘
‘; // 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’);i am seeing the
‘; // 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’);
infront the actual page .
the text is visible in the page
i see it working now
What we needed to do is to add
`<Counts>
<Count>
<CnTrafficType>all</CNTrafficType>
<NumberOfMessages>4</NumberOfMessages>
<LenghtOfevents>2</LenghtOfevents>
</Count>
<Count>
<CNTrafficType>0<CNTrafficType>
<NumberOfMessages>4</NumberOfMessages>
</Count>
</Counts>You can add the above from the main plugin file i would also advice leaving the MINUTE_IN_SECONDS to around 20 mins so you don’t get rate limited by the API for making too many request within a short time.
You can download the updated plugin here.
-
This reply was modified 1 year, 7 months ago by
The topic ‘XML with api to wordpress’ is closed to new replies.