1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Adding PHP, with parameters, to a page node.

Discussion in 'XenForo Development Discussions' started by BigBoomer, Aug 13, 2014.

  1. BigBoomer

    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.
     
  2. MattW

    MattW Well-Known Member

    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 $controllerXenForo_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($resptrue);
                    
    $response->params['details'] = $details;
            }
    }
    Page node
    upload_2014-8-13_23-32-40.png

    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.png
     
    eva2000 and BigBoomer like this.
  3. BigBoomer

    BigBoomer Member

    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!!!
     
    MattW likes this.
  4. MattW

    MattW Well-Known Member

    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;
     
    BigBoomer likes this.
  5. BigBoomer

    BigBoomer Member

    Perfect!!! Thanks very much for the help!!
     
    MattW likes this.
  6. BigBoomer

    BigBoomer Member

    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 $controllerXenForo_ControllerResponse_Abstract &$response)
            {

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

                    
    $parsed_json json_decode($json_stringtrue);

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

    ?>
     
  7. MattW

    MattW Well-Known Member

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

    and you have the file / directory structure /library/BPLeaderboard/ControllerPublic/bpMedals.php ?
     
    eva2000 likes this.
  8. MattW

    MattW Well-Known Member

    You are also missing a ; from your code here
    PHP:
    $response->params['bpLB'] = $bpLeaderboard
     
    eva2000 likes this.
  9. BigBoomer

    BigBoomer Member

    Thanks, traced folders and found a typo. Works now.
     
    MattW likes this.

Share This Page