• Goos

    (@meganmante)


    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

    The page I need help with: [log in to see the link]

Viewing 15 replies - 16 through 30 (of 38 total)
  • Thread Starter Goos

    (@meganmante)

    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

    Thread Starter Goos

    (@meganmante)

    to understand it better it is now that I then
    must use
    [anwb_traffic_info $message->FullText]

    Thread Starter Goos

    (@meganmante)

    I see it is working now ,

    How can I make sure there is a space in between?
    and the road information in a separate column

    Thread Starter Goos

    (@meganmante)

    How can I make sure there is a space in between?
    and the road information in a separate column

    and 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.6

    How can we set this up

    @meganmante

    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');
    • This reply was modified 3 weeks, 1 day ago by tarhe.
    • This reply was modified 3 weeks, 1 day ago by tarhe.
    Thread Starter Goos

    (@meganmante)

    Hi will let you know today thanks for letting me know

    Goos

    Thread Starter Goos

    (@meganmante)

    how can we reset the update time to 2 min .

    Thread Starter Goos

    (@meganmante)

    And one more question where do i have to put the css script in to elementor

    Thread Starter Goos

    (@meganmante)

    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

    @meganmante

    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_expiration variable to use MINUTE_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 3 weeks, 1 day ago by tarhe.
    Thread Starter Goos

    (@meganmante)

    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’);

    Thread Starter Goos

    (@meganmante)

    Thread Starter Goos

    (@meganmante)

    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

    Thread Starter Goos

    (@meganmante)

    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.

Viewing 15 replies - 16 through 30 (of 38 total)
  • You must be logged in to reply to this topic.