Adding PHP, with parameters, to a page node.

BigBoomer

Member
This is my first attempt at doing any custom coding for XenForo, so please bare with me. I have some PHP code that pulls JSON data from an API. I would like to know the proper way to setup the callback class and functions. I would like to be able to pass parameters to these callbacks to display a different set of data depending on the parameters, to minimize the number of templates and includes in the site. I am new to PHP, but have created the code needed. Just need help putting it in my forum. Thanks all in advance!!

P.S. - I have searched many tutorials already, but have not found ones for all features I need.
 
This is one I did for a basic page to pull server stats from an API

I created the directory /library/MWSServerStats/ControllerPublic and called the file ServerStats.php

PHP:
<?php

class MWSServerStats_ControllerPublic_ServerStats
{
        public static function getStats(XenForo_ControllerPublic_Abstract $controller, XenForo_ControllerResponse_Abstract &$response)
        {
                $api = "MYKEY";

                $curl = curl_init();
                curl_setopt_array($curl, array(
                CURLOPT_RETURNTRANSFER => 1,
                CURLOPT_URL => 'https://nodequery.com/api/servers?api_key='.$api.'',
                CURLOPT_USERAGENT => 'XenForo cURL Request',
                ));
                $resp = curl_exec($curl);
                curl_close($curl);
                $details = json_decode($resp, true);
                $response->params['details'] = $details;
        }
}

Page node
upload_2014-8-13_23-32-40.webp

template HTML
HTML:
<xen:foreach loop="$details.data" value="$server">
</xen:foreach>
<xen:foreach loop="$server" value="$usage">
<div id="MWSServerStats" class="section">
<div class="secondaryContent statsList">
<div class="pairsJustified">
<dl><span class="label"><dt>Server Name:</dt></span><dd>{$usage.name}</dd></dl>
<dl><span class="label"><dt>Load Percentage:</dt></span><dd>{$usage.load_percent}</dd></dl>
<dl><span class="label"><dt>Load Average:</dt></span><dd>{$usage.load_average}</dd></dl>
<dl><span class="label"><dt>RAM Usage:</dt></span><dd>{xen:number {xen:calc "{$usage.ram_usage} / 1024 / 1024 / 1024"},2} GB of {xen:number {xen:calc "{$usage.ram_total} / 1024 / 1024 / 1024"},2} GB</dd></dl>
<dl><span class="label"><dt>Disk Usage:</dt></span><dd>{xen:number {xen:calc "{$usage.disk_usage} / 1024 / 1024 / 1024"},2} GB of {xen:number {xen:calc "{$usage.disk_total} / 1024 / 1024 / 1024"},2} GB</dd></dl>
<dl><span class="label"><dt>Average Network TX:</dt></span><dd>{xen:number {xen:calc "{$usage.current_tx} / 180 / 1024"},2} KB/s</dd></dl>
</div>
</div>
</div>
</xen:foreach>

upload_2014-8-13_23-34-37.webp
 
This looks to be exactly what I need, AWESOME!!. I would like to ask though, is there a way I can make this dynamic? I am polling the API of an online game to get the leaderboards. The company has several different games, so I can pass game ID's to get the different games' info. Not a big deal for the moment, but definitely something I need after I polish off the hard-coded version. Thanks for your help!!!
 
You could set an option via the ACP, which you can call via the php function

PHP:
$options = XenForo_Application::get('options');
$scEnabled = $options->dailystats_sc_enable;
 
OK, I created a class and when I try to implement the callback, it says it's an invalid class. Here is my code:

PHP:
<?php

class BPLeaderboard_ControllerPublic_bpMedals
{
        public static function getLeaderboards(XenForo_ControllerPublic_Abstract $controller, XenForo_ControllerResponse_Abstract &$response)
        {

                $json_string =    file_get_contents("http://foo");

                $parsed_json = json_decode($json_string, true);

                $bpLeaderboard = $parsed_json;
               
                $response->params['bpLB'] = $bpLeaderboard
        }
}

?>
 
So in the page node, you are putting: BPLeaderboard_ControllerPublic_bpMedals::getLeaderboards

and you have the file / directory structure /library/BPLeaderboard/ControllerPublic/bpMedals.php ?
 
You could set an option via the ACP, which you can call via the php function

PHP:
$options = XenForo_Application::get('options');
$scEnabled = $options->dailystats_sc_enable;
Sorry to dig out an old thread.

I want to have 2 radio buttons in my custom page, and want to fetch different data based on the selection (by the user). So, I need to send the selection value to my ControllerPublic class.

How can I do that (or is it possible to do that)?

Thanks,
 
Back
Top Bottom