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

What am I doing wrong? Not able to using variables from php in template.

arcaneex

Active member
#1
Long story short , here's my listener:
PHP:
<?php
class Steamrep_Rep_Rep {
public static function includePhpFile($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
    {
        if($hookName == 'message_user_info_avatar')
        {
            ob_start();
            require_once('repchecker.php');
            $contents .= ob_get_contents();
            ob_end_clean();
                        $hookParams['rep'] = $rep;
                        $params = $template->getParams();
                        $params += $hookParams;
        }
    }
}
?>
My PHP file:
PHP:
<?php
class Steam_Rep_Rep {
 
    // cURL Variable
    private $ch = null;
 
    public function __construct() {
        // Setup cURL
        $this->ch = curl_init();
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($this->ch, CURLOPT_TIMEOUT, 4);
    }   
    public function getUserInfo($steamrep_id) {
        // cURL
        curl_setopt($this->ch, CURLOPT_URL, "http://steamrep.com/api/beta/reputation/76561197971691194?json=1");
        $result = curl_exec($this->ch);
        $xml = json_decode($result);
        if(!empty($xml->steamrep->reputation)) {
            return array(
                'rep' => $xml->steamrep->reputation,
            );
        } else {
            return array(
                'rep' => "No reputation found",
            );
        }
    }
}
$steam = new Steam_Rep_Rep();
$rep = $steam->getUserInfo('76561197971691194');
?>
Basically , what I need to do is output the content of this link: http://steamrep.com/api/beta/reputation/76561197971691194?json=1 in message_user_info.
Can anyone help?
 

Chris D

XenForo developer
Staff member
#4
As per my post yesterday, you have something missing.

Ideally you need to create your own template that is inserted into the hook, and pass along with that the paramters. Right now the parameters you're defining aren't being passed to anything.

Your Listener needs to look like this:


PHP:
<?php
class Steamrep_Rep_Rep {
public static function includePhpFile($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
    {
        if($hookName == 'message_user_info_avatar')
        {
            ob_start();
            require_once('repchecker.php');
            ob_end_clean();
                        $hookParams['rep'] = $rep;
                        $params = $template->getParams();
                        $params += $hookParams;
            $contents .= $template->create('steam_rep', $params);
        }
    }
}
?>
Then in your steam_rep template you can now use:

{$rep.rep}

For me, that looks like this:

steam_rep.PNG
 

arcaneex

Active member
#5
As per my post yesterday, you have something missing.

Ideally you need to create your own template that is inserted into the hook, and pass along with that the paramters. Right now the parameters you're defining aren't being passed to anything.

Your Listener needs to look like this:


PHP:
<?php
class Steamrep_Rep_Rep {
public static function includePhpFile($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
    {
        if($hookName == 'message_user_info_avatar')
        {
            ob_start();
            require_once('repchecker.php');
            ob_end_clean();
                        $hookParams['rep'] = $rep;
                        $params = $template->getParams();
                        $params += $hookParams;
            $contents .= $template->create('steam_rep', $params);
        }
    }
}
?>
Then in your steam_rep template you can now use:

{$rep.rep}

For me, that looks like this:

View attachment 33555
Thank you so much but it doesn't show what you have.
http://prntscr.com/es9nq
I didn't have steam_rep , I suppose it needs to be created by the listener but it didn't so I manually created it and it still doesn't work.