View Full Version : PHP API code in template, php file, or somewhere else?
petteyg359
01-16-2008, 03:53 AM
I have the following PHP code:<?
require_once('./api/class.api.php');
require_once('./api/class.charselect.php');
$api = new Api();
$api->debug(false);
$api->cache(false);
$api->setCredentials($apiuser,$apipass);
$uchars = $api->getCharacters();
$chars = CharSelect::getCharacters($uchars);
$apichar = $chars[0]['charname'];
$api->setCredentials($apiuser,$apipass,$apichar);
?>
I need to use this, and several more $api->getSomeInfo calls, in a new template I've created. I've got the template and php file and a link to it in the footer bar all working, I just need to know where to put this code, so that it will run when the page is accessed and so I can call the $api functions as needed from the template. Or should I run this in the php file itself and grab everything it might need, then just have the template display the requested parts? Also, if will need <if condition=$bbuserinfo[fieldX]> stuff, and I've read that doesn't work well outside of the template. Does that mean that I _can't_ put the code into the php file?
Dismounted
01-16-2008, 04:00 AM
You will need to create a plugin at the appropriate location as you cannot run PHP code in templates directly.
petteyg359
01-16-2008, 02:45 PM
This will need to be called many times with different settings. Do I need a seperate plugin for each call ($359api_chars, $359api_tree, etc) or can the plugin be used as an array? I followed the instructions from https://vborg.vbsupport.ru/showthread.php?t=119933&highlight=plugin, but when I put $359eveit[1][charname] in the display template, it actually shows the variable name, rather than nothing. Considering it should be a blank variable, it should display nothing, right?
Opserty
01-16-2008, 03:34 PM
PHP variable names cannot begin with a number.
petteyg359
01-16-2008, 04:03 PM
Thank you, having all these problems and never thought of that :)
Alright, I've changed everything around so it is now $eveit359. Plugin output is now getting to the template, as long as it's plain HTML. When I try to make $eveit359 into an array (store a value into $eveit359[1], and put that in the template, I get output like "p[charname]". I am completely lost trying to figure out what the heck the template/plugin tutorials are saying with variable renaming or whatever it is they're talking about. I need to have several arrays of data accessible to the template, preferably in one big array $eveit359[1][charname], $eveit359[1][charid], $eveit359[1][tree][current], $eveit359[1][tree][time], etc.
Opserty
01-16-2008, 05:20 PM
I don't know might be something to do with your script. Arrays start from 0 not 1. I don't know why you are storing it in arrays you can store it as a variable normally. If it makes it easier to use arrays then use arrays. If you have multidimension arrays consider surrounding it in brackets. Like in your template have:
{$eveit359[1][tree][time]}
If you run this at the end of your plugin it will give you a dump of all the information stored in the variable.
var_dump($eveit359);
petteyg359
01-16-2008, 05:40 PM
My current plugin template content: <?php
require_once('./eveitapi/class.api.php');
require_once('./eveitapi/class.charselect.php');
$api359 = new Api();
$api359->debug(false);
$api359->cache(true);
$api359->setCredentials($apiuser359,$apipass359);
$uchars359 = $api359->getCharacters();
$chars359 = CharSelect::getCharacters($uchars359);
$eveit359 = $chars359[0][charname];
var_dump($eveit359);
?>
On the display template, rather than showing the contents of $eveit359 (as I would expect it to from var_dump), it outputs the php script as plain text.
I tried changing the plugin template to a simple <?php
$eveit359 = "Hello";
var_dump($eveit359);
?> and got no output. <?php
echo "Hello";
?> also produces no output.
Not sure if what you meant was to use another variable in the plugin and display templates, but I tried <?php $testing359="test"; ?> and $testing359 in the display template got no output.
Could the Api functions be causing a problem? The plain text script output starts with "debug(false); $api359->cache(true);" as if that first "->" is a problem.
--------------- Added 1200517434 at 1200517434 ---------------
I just tried <?php
echo "<font color=white>Hello</font>";
?>
as the plugin template, and now the display template outputs the following in view source: <?php
echo \"<font color=white>Hello</font>\";
?>
On the page it appears as Hello\"; ?>
If I change the double quotes to single quotes, output is blank.
Dismounted
01-17-2008, 03:13 AM
You don't need to have <?php and ?> in plugins, as the code is evaluated (using eval()).
petteyg359
01-17-2008, 02:28 PM
New plugin template: require_once('./eveitapi/class.api.php');
require_once('./eveitapi/class.charselect.php');
$api359 = new Api();
$api359->debug(false); // enable debugging
$api359->cache(false); // do not use a cache-file, if one exists
$api359->setCredentials($bbuserinfo[field6],$bbuserinfo[field7]);
$uchars359 = $api359->getCharacters();
$chars359 = CharSelect::getCharacters($uchars359);
$eveitapi = $chars359[0][charname];
$charblahtest = "Hello";
echo "Hello";
New output:require_once('./eveitapi/class.api.php'); require_once('./eveitapi/class.charselect.php'); = new Api(); (false); // enable debugging (false); // do not use a cache-file, if one exists (1188020,RDQiA46jtGtzMUEFVtgbtJ2CfhTCIPGkD0JeUz22N omPeQWCx552fwe0gwkSi4bh); = (); = CharSelect::getCharacters(); = [charname]; = "Hello"; echo "Hello";
It's still pretending it's plain text rather than executing the PHP code.
The above is using the "eval" plugin and "array_merge" cache plugin.
When I use the method from this post (https://vborg.vbsupport.ru/showpost.php?p=1264090&postcount=16) I get the following output. Still doesn't parse it, but at least this way it's not evaluating the variables.
require_once('./eveitapi/class.api.php'); require_once('./eveitapi/class.charselect.php'); $api359 = new Api(); $api359->debug(false); // enable debugging $api359->cache(false); // do not use a cache-file, if one exists $api359->setCredentials(" . $GLOBALS['vbulletin']->userinfo['field6'] . "," . $GLOBALS['vbulletin']->userinfo['field7'] . "); $uchars359 = $api359->getCharacters(); $chars359 = CharSelect::getCharacters($uchars359); $eveitapi = $chars359[0][charname]; $charblahtest = \"Hello\"; echo \"Hello\";
Would it be easier to just build the variables and/or array in eveit.php itself, rather than using another template, since the API part won't be displaying anything, just storing information?
Marco van Herwaarden
01-17-2008, 02:47 PM
What editor are you using? Looks like all linebreaks are removed.
Opserty
01-17-2008, 02:49 PM
Just to clarify you are placing this code in a PLUGIN correct? (AdminCP > Plugins and Products > Add New Plugin)
And not a TEMPLATE? (AdminCP > Style Manger....)
You keep saying plugin template its confusing as to what you mean exactly as they are two different things.
petteyg359
01-17-2008, 02:58 PM
The plugin article said a plugin is a variable that display the contents of another template. I've been putting the code in this second template. Should the code be in the plugin itself? The plugin code is currently $eveitapi = fetch_template('eveitapi'); So if I put all the PHP in the plugin itself, I should be able to use the defined variables on any template (since the plugin is in global_start)? When I said plugin template I was referring to the template the plugin calls. When I said display template I was referring to the template that had $eveitapi where I wanted the plugin stuff to display.
Opserty
01-17-2008, 03:02 PM
I'm not sure what article you were reading but a Plugin is a peice of PHP Code that is excuted at a certain location (the hook location) by vBulletin. You PHP Code must go inside a Plugin.
You can use a Plugin to fetch a template, a template is front-end HTML that is displayed to the user. You can use variables in your template that get their values from the PHP code, but you cannot execute PHP code inside templates.
Think of it like this (this is just a demonstration of how the system works you do not need to use this code at all!):
// This is just some default vBulletin PHP file
// This is where you launch your api and get your data
eval('YOUR PLUGIN CODE WILL APPEAR HERE');
echo "YOUR TEMPLATE CODE WILL BE HERE AND IT IS OUTPUTTED TO THE USER";
// End of the Script
petteyg359
01-17-2008, 03:26 PM
I tried putting the code in a plugin with hook global_start but variables weren't available to the template. When I put the code directly in the php file things work. Are there any security issues with putting the code directly in the php file?
Opserty
01-17-2008, 03:51 PM
What template was it? and make sure you checked that the variables actually had some data in them. Also make sure you eval()'d the template like this (if you are using a custom one)
eval('$somevar = "'. fetch_template('your_template') .'";'); Then you would place $somevar in a normal vBulletin template and it would display your code there.
I doesn't make much difference if you place your code in Plugins or PHP files, but the plugin system is there so you don't have to mess with Files.
petteyg359
01-17-2008, 04:23 PM
The file is 'eveit.php'. The template is 'eveit'. It is working just fine with the API code in the PHP file. The template is called at the very bottom of the PHP file eval('print_output("' . fetch_template('eveit') . '");'); I deleted the second template and tried the code in a plugin instead, but I think it is better in the PHP rather than a global_start plugin, as I only want the API access performed on eveit.php (the rest of the site doesn't make use of the data). Is it possible to make a plugin load only for a specified custom template?
Dismounted
01-18-2008, 03:54 AM
If the code you're wanting to put is in your own custom file, by all means, put that code in your file. Plugins are mainly for vBulletin so that when you update, your edits aren't lost.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2024, vBulletin Solutions Inc.