MyBB Community Forums

Full Version: Having severe problems pulling info from an API in a plugin
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to create a plugin that pulls Battlefield: bad Company 2 stats from an API. I've created a working stand-0alone PHP file that I use on ym site, but I'm now trying to create a plugin that displays a user's stats within their profiles.

The problem is, I can't seem to get the plugin to actually pull any data from the API. I have it set up so that the plugin creates a new user profile field for their XBox live Gamertag. The text within this new field is then used to call the API. I think it's here that the troubles are originating.

Anyway, I'd be really grateful if somebody could take a look at the code (which I've provided below) and help me out a little.



<?php
/**
 * MyBC2
 * Created by euantor using the bfbcs.com API
 */
 
// Disallow direct access to this file for security reasons DO NOT REMOVE
if(!defined("IN_MYBB"))
{
    die("This file cannot be directly accessed. Please return to the main site - nothing to see here.");
}

$plugins->add_hook("member_profile_start", "mybc2_calc");
$plugins->add_hook("member_profile_end", "mybc2_prof");

function mybc2_info()
{
	return array(
		"name"			=> "mybc2",
		"description"	=> "Display a user's BC2 stats in their profile.",
		"website"		=> "http://www.360elites.net",
		"author"		=> "euantor",
		"authorsite"	=> "http://www.360elites.net",
		"version"		=> "0.1",
		"guid" 			=> "",
		"compatibility" => "*"
	);
}

function mybc2_activate() 
{
	global $mybb, $db;
	
	$profilefield = array(
		"fid"			=> "",
		"name"			=> "gamertag",
		"description"	=> "Your xbox live gamertag.",
		"type"			=> "text",
		"maxlength"		=> "50",
		"editable"		=> "1",
		"hidden"		=> "1",
	);
	
	$db->insert_query("profilefields", $profilefield);
	$pfid = $db->insert_id();
	$db->write_query("ALTER TABLE ".TABLE_PREFIX."userfields ADD `fid{$pfid}` TEXT");
		
	
	    $settings_group = array(
        "gid" => "",
        "name" => "mybc2",
        "title" => "mybc2",
        "description" => "Settings for the mybc2 plugin.",
        "disporder" => "48",
        "isdefault" => "0",
        );
		
    $db->insert_query("settinggroups", $settings_group);
    $gid = $db->insert_id();
	
	$setting_1 = array(
		"name"			=> "mybc2prof",
		"title"			=> "Enable plugin?",
		"description"	=> "Do you want to enable the mybc2 plugin?",
		"optionscode"	=> "yesno",
		"value"			=> "1",
		"disporder"		=> "1",
		"gid"			=> $gid,
	);
	
		$db->insert_query("settings", $setting_1);
		
		
		$setting_2 = array(
		"name"			=> "mybc2pid",
		"title"			=> "gamertag profile field ID",
		"description"	=> "This is the profile field ID of your gamertag profile field.",
		"optionscode"	=> "text",
		"value"			=> $pfid,
		"disporder"		=> "2",
		"gid"			=> $gid,
	);
	
		$db->insert_query("settings", $setting_2);
	
		
			$template_1 = array(
		"title"		=> 'mybc2',
		"template"	=> '<br />
<table border="0" cellspacing="{$theme[\\\'borderwidth\\\']}" cellpadding="{$theme[\\\'tablespace\\\']}" class="tborder">
<tr>
<td colspan="2" class="thead"><strong>My Battlefield Bad Company 2 Stats</strong></td>
</tr>
<tr>
<td class="trow1">
Accuracy: {$accuracy}
</td>
</tr>
</table>',
		"sid"		=> "-1",
		"version"	=> "1.0",
		"dateline"	=> TIME_NOW
	);

	$db->insert_query("templates", $template_1);
	
	require "../inc/adminfunctions_templates.php";
	find_replace_templatesets("member_profile", '#{\$signature}#', "{\$signature}\n{\$mybc2}");
	
 rebuild_settings();

}

function mybc2_deactivate() 
{
	global $mybb, $db;
	
	    $query = $db->simple_select("settinggroups", "gid", "name='mybc2'");
    	$gid = $db->fetch_field($query, 'gid');
		$db->delete_query("settinggroups","gid='".$gid."'");
		$db->delete_query("settings","gid='".$gid."'");
		$db->delete_query("templates","title='mybc2'");
		
		$query2 = $db->simple_select("profilefields", "fid", "name='gamertag'");
    	$fid = $db->fetch_field($query2, 'fid');
		$db->delete_query("profilefields", "fid='{$fid}'");
		$db->write_query("ALTER TABLE ".TABLE_PREFIX."userfields DROP fid{$fid}");
		
		require "../inc/adminfunctions_templates.php";
		find_replace_templatesets("member_profile", '#'.preg_quote('{$mybc2}').'#', '',0);
		
    rebuild_settings();

}

function mybc2_calc()
{

#Pure calculations etc - just getting prepped for the script

$mybc2id = $userfields['fid'.$mybb->settings['mybc2pid']];

$url = 'http://api.bfbcs.com/api/360';
$postdata = 'players='.$mybc2id.'&fields=all';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$data = curl_exec($ch);
curl_close($ch);

$data = json_decode($data,true);

#Working out K/D

$KD = $data[players][0][kills] / $data[players][0][deaths];

#Rounding K/D to 2DP

$KD = round($KD, 2);

#Finding games played

$played = $data[players][0][general][wins] + $data[players][0][general][losses];

#Working out W/L

$WL = $data[players][0][general][wins] / $data[players][0][general][losses];

#Rounding W/L to 2DP

$WL = round($WL, 2);

#Working out the time - converting from hours to days, hours, minutes ;)

#work out th number of days spent - seeing as the time is provided in seconds, we must divide by a big number ;D

$days = (($data[players][0][time] / 60) / 60) / 24;

#Since time is provided ins econds, we must see how many seconds are left after working out days D:

$secondsleftafterdays = $data[players][0][time] % 86400;

$hours = $secondsleftafterdays / 3600;



#calculate accuracy

$accuracy = ($data[players][0][general][accuracy]) * 100;
}


function mybc2_prof() 
{
	global $mybb, $theme, $mybc2, $templates, $userfields;

	$mybc2id = $userfields['fid'.$mybb->settings['mybc2pid']];

	if(!$mybb->settings['mybc2prof'] || !$mybc2id)
		return;

		eval("\$mybc2 = \"".$templates->get("mybc2")."\";");

}


?>
Any data calculated in mybc2_calc is not being used in mybc2_prof
You need to set global $accuracy; in both functions
Ah. Didn't realize that. Major oops on my part right there.


EDIT: Still no luck. I'm also getting errors about dividing by zero in mybb_calc - which tells me that the data just isn't being fetched at all at the minute.
Okay, just solved the problem. I made a very stupid spelling error early on >.>