XF 2.2 How I could display XML data on Xenforo page?

Solution
Create a simple add-on (or simply create a folder in src/addons directory) called Weather.

And create a PHP file in this directory with the following content: Weather.php
PHP:
<?php
namespace Weather;

class Weather
{...

smozgur

Active member
Create a simple add-on (or simply create a folder in src/addons directory) called Weather.

And create a PHP file in this directory with the following content: Weather.php
PHP:
<?php
namespace Weather;

class Weather
{
    public static function index(\XF\Pub\Controller\AbstractController $controller, \XF\Mvc\Reply\AbstractReply &$reply)
    {
        $xml = simplexml_load_file('https://klima.hr/agro_regije_prognoza.xml');

        foreach ($xml->children() as $child) {
            if ($child->getName() == 'datum') {
                $date = $child;
            } elseif (isset($child['ime'])) {
                $regions[] = $child;
            }
        }

        $viewParams = [
            'date' => $date,
            'regions' => $regions,
        ];

        $reply->setParams($viewParams);
    }
}
See how I set up $viewParams to be used in the view template.

You are done with PHP that loads the remote XML file and parses it. Now it is time to create the XenForo page and make the connection between the page and the PHP callback.

Go to XenForo Admin->Forums->Nodes and click Add Node. Set the parameters in the first screenshot below as you need:
page-properties.png

The second screenshot below is more important. The template that will show the view parameters coming from the PHP callback, and the path for the callback function. You can design the template as you desire by following the sample of how I did.
page-properties-2.png

Template:
HTML:
<h2>
    Datum: {$date}
</h2>
<xf:foreach loop="$regions" key="$key" value="$region" i="$i">
    <h3>
        Regija: {$region.ime}
    </h3>
    <p>
        {$region}
    </p>
</xf:foreach>

PHP Callback:
Weather\Weather -> the first one is the folder name in the addons directory, the second one is the PHP class name.
index -> function name in the class.

Save the page, and go to the forum to click on the page in the nodes list. It shows the following in my implementation as I explained step by step above.

1602965279542.png

It can be also created as a custom widget, but it is different than setting up it as a page. I believe this will provide a starting point.

Please note this is not the preferred method, making the remote call every time the page is displayed. Instead of that, you should cache this info somewhere (preferably in the database) and serve from the database instead of reading the remote source. Then you can refresh the data in the database periodically. Although the XenForo page cache will also do a similar caching, it will be only for a less time period. However, you'll likely need this info to be updated every 24 hours. So, I strongly recommend building your cache mechanism for this purpose.

Hope it helps.
 
Solution
Top