PHP Class JSon Returns etc. etc.

API Discussion
so i busted my !@# on new site...
will not give all source codes free ... but i know many other people struggle with this information.
so here's a prime class i did as an example for all of u

<?php

class sentaPedia {
/* private construct */
private $region;
private $realm;
private $char;
private $fields;
/* public construct */
public $cInfo = array();
public $iChar = array();

public $dClass = array();
public $dRace = array();
public $dPower = array();

public $sDef = array();
public $sEnh = array();

/* *******************
* Class Constructor *
* *******************/
function __construct($reg, $rlm, $chr) {
$this->region = strtolower($reg);
$this->realm = str_replace(" ", "%20", strtolower($rlm));
$this->fields = "?fields=stats,items,guild,progression,talents";
$this->char = urlencode($chr);
$this->loadData();
}

private function loadData() {
$urlLink = "http://" . $this->region . ".battle.net/api/wow/character/" . $this->realm . "/" . $this->char . $this->fields;
$this->cInfo = json_decode($this->getStatusPage($urlLink), true);
// print_r($this->cInfo);
$this->setBuild();
}
private function setBuild() {
if ($this->cInfo['talents']['0']['selected'] == 1) {
$this->iChar['name'] = $this->cInfo['talents']['0']['spec']['name'];
$this->iChar['role'] = $this->cInfo['talents']['0']['spec']['role'];
} else {
$this->iChar['name'] = $this->cInfo['talents']['1']['spec']['name'];
$this->iChar['role'] = $this->cInfo['talents']['1']['spec']['role'];
}
$this->iChar['role'] = strtolower($this->iChar['role']); // healing, dps, tank

$this->dClass = $this->getClassInfo($this->cInfo['class']);
$this->dRace = $this->getRaceInfo($this->cInfo['race']);
$this->dPower = $this->returnPower($this->cInfo['stats']);

$this->sDef['armor'] = $this->cInfo['stats']['armor'];
$this->sDef['dodge'] = $this->returnDodge($this->cInfo['stats']);
$this->sDef['parry'] = $this->returnParry($this->cInfo['stats']);
$this->sDef['block'] = $this->returnBlock($this->cInfo['stats']);

$this->sEnh['crit'] = $this->returnCrit($this->cInfo['stats']);
$this->sEnh['haste'] = $this->returnHaste($this->cInfo['stats']);
$this->sEnh['mastr'] = $this->returnMastery($this->cInfo['stats']);
$this->sEnh['barmr'] = $this->cInfo['stats']['bonusArmor'];
$this->sEnh['multi'] = $this->returnMultistrike($this->cInfo['stats']);
$this->sEnh['leech'] = $this->returnLeech($this->cInfo['stats']);
$this->sEnh['versa'] = $this->returnVersatility($this->cInfo['stats']);
$this->sEnh['avoid'] = $this->returnAvoidance($this->cInfo['stats']);
}
public function avatarName() {
$avaName = "<a class=\"guild-label\" target=\"_blank\" onMouseover=\"ddrivetip('<b>" . $this->cInfo['name'] . "</b><br />" . $this->cInfo['realm'] . "','#333333', 250);\" onMouseout=\"hideddrivetip();\" href=\"http://" . $this->region . ".battle.net/wow/en/character/" . strtolower($this->cInfo['realm']) . "/" . str_replace(" ", "_", $this->cInfo['name']) . "/advanced\">" . $this->cInfo['name'] . "</a>";
return $avaName;
}
public function guildData() {
if (!$this->cInfo['guild']['name']) {
$guildData = "<b>No Guild</b>";
}
else {
$guildData = "<a class=\"guild-label\" target=\"_blank\" onMouseover=\"ddrivetip('<b>" . $this->cInfo['guild']['name'] . "</b><br />" . $this->cInfo['realm'] . "','#333333', 250);\" onMouseout=\"hideddrivetip();\" href=\"http://" . $this->region . ".battle.net/wow/en/guild/" . strtolower($this->cInfo['realm']) . "/" . str_replace(" ", "_", $this->cInfo['guild']['name']) . "/?character=" . strtolower($this->cInfo['name']) . "\">" . $this->cInfo['guild']['name'] . "</a>";
}
return $guildData;
}

private function getStatusPage($url){
if (!$url) { die(); }
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_FRESH_CONNECT,TRUE);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch,CURLOPT_HTTPHEADER,array("Content-Type: text/xml; charset=utf-8","Expect: 100-continue"));
curl_setopt($ch,CURLOPT_REFERER,'http://www.google.ch/');
curl_setopt($ch,CURLOPT_TIMEOUT,10);
$html=curl_exec($ch);
if($html==false){
$m=curl_error(($ch));
error_log($m);
}
curl_close($ch);
return $html;
}

public function getRaceInfo($q) {
switch($q) {
case "1":
$race['name'] = "Human";
$race['fact'] = "Alliance";
break;
case "2":
$race['name'] = "Orc";
$race['fact'] = "Horde";
break;
case "3":
$race['name'] = "Dwarf";
$race['fact'] = "Alliance";
break;
case "4":
$race['name'] = "Night Elf";
$race['fact'] = "Alliance";
break;
case "5":
$race['name'] = "Undead";
$race['fact'] = "Horde";
break;
case "6":
$race['name'] = "Tauren";
$race['fact'] = "Horde";
break;
case "7":
$race['name'] = "Gnome";
$race['fact'] = "Alliance";
break;
case "8":
$race['name'] = "Troll";
$race['fact'] = "Horde";
break;
case "9":
$race['name'] = "Goblin";
$race['fact'] = "Horde";
break;
case "10":
$race['name'] = "Blood Elf";
$race['fact'] = "Horde";
break;
case "11":
$race['name'] = "Draenei";
$race['fact'] = "Alliance";
break;
case "22":
$race['name'] = "Worgen";
$race['fact'] = "Alliance";
break;
case "24":
$race['name'] = "Pandaren";
$race['fact'] = "Neutral";
break;
case "25":
$race['name'] = "Pandaren";
$race['fact'] = "Alliance";
break;
case "26":
$race['name'] = "Pandaren";
$race['fact'] = "Horde";
break;
default:
$race['name'] = "Unknown";
$race['fact'] = "Neutral";
}
return $race;
}

public function getClassInfo($q) {
switch($q) {
case "1":
$class['name'] = "Warrior";
$class['clrs'] = "#C79C6E"; // Tan
break;
case "2":
$class['name'] = "Paladin";
$class['clrs'] = "#F58CBA"; // Pink
break;
case "3":
$class['name'] = "Hunter";
$class['clrs'] = "#ABD473"; // Green
break;
case "4":
$class['name'] = "Rogue";
$class['clrs'] = "#FFF569"; // Light Yellow
break;
case "5":
$class['name'] = "Priest";
$class['clrs'] = "#FFFFFF"; // White
break;
case "6":
$class['name'] = "Death Knight";
$class['clrs'] = "#C41F3B"; // Red
break;
case "7":
$class['name'] = "Shaman";
$class['clrs'] = "#0070DE"; // Blue
break;
case "8":
$class['name'] = "Mage";
$class['clrs'] = "#69CCF0"; // Light Blue
break;
case "9":
$class['name'] = "Warlock";
$class['clrs'] = "#9482C9"; // Purple
break;
case "10":
$class['name'] = "Monk";
$class['clrs'] = "#00FF96"; // Jade Green
break;
case "11":
$class['name'] = "Druid";
$class['clrs'] = "#FF7D0A"; // Orange
break;
default:
$class['name'] = "Unknown";
$class['clrs'] = "#000000"; // Black
}
return $class;
}

public function returnPower($q) {
switch($q['powerType']) {
case "runic-power":
$power[0] = "Runic";
$power[1] = "runic-power";
break;
case "energy":
$power[0] = "Energy";
$power[1] = "energy";
break;
case "mana":
$power[0] = "Mana";
$power[1] = "mana";
break;
case "focus":
$power[0] = "Focus";
$power[1] = "focus";
break;
case "rage":
$power[0] = "Rage";
$power[1] = "rage";
break;
default:
$power[0] = "Mana";
$power[1] = "mana";
}
$power[2] = $q['power'];
$power['health'] = $q['health'];
return $power;
}
function returnDodge($q) {
$dodge[0] = round($q['dodge'], 2);
$dodge[1] = round($q['dodgeRating'], 2);
$dodge[2] = $dodge[0] - 5;
$dodge[0] = number_format((float)$dodge[0], 2, '.', '');
$dodge[2] = number_format((float)$dodge[2], 2, '.', '');
return $dodge;
}
public function returnParry($q) {
$parry[0] = round($q['parry'], 2);
$parry[1] = round($q['parryRating'], 2);
$parry[2] = $parry[0] - 5;
$parry[0] = number_format((float)$parry[0], 2, '.', '');
$parry[2] = number_format((float)$parry[2], 2, '.', '');
return $parry;
}
public function returnBlock($q) {
$block[0] = round($q['block'], 2);
$block[1] = round($q['blockRating'], 2);
$block[2] = $block[0];
$block[0] = number_format((float)$block[0], 2, '.', '');
$block[2] = number_format((float)$block[2], 2, '.', '');
return $block;
}
public function returnCrit($q) {
$crit[0] = round($q['crit'], 2);
$crit[1] = round($q['critRating'], 2);
$crit[2] = $crit[0] - 5;
$crit[0] = number_format((float)$crit[0], 2, '.', '');
$crit[2] = number_format((float)$crit[2], 2, '.', '');
return $crit;
}
public function returnHaste($q) {
$haste[0] = round($q['haste'], 2);
$haste[1] = round($q['hasteRating'], 2);
$haste[2] = round($q['hasteRatingPercent'], 2);
$haste[0] = number_format((float)$haste[0], 2, '.', '');
$haste[2] = number_format((float)$haste[2], 2, '.', '');
return $haste;
}
public function returnMastery($q) {
$mastery[0] = round($q['mastery'], 2);
$mastery[1] = round($q['masteryRating'], 2);
$mastery[0] = number_format((float)$mastery[0], 2, '.', '');
return $mastery;
}
public function returnMultistrike($q) {
$multistrike[0] = round($q['multistrike'], 2);
$multistrike[1] = round($q['multistrikeRating'], 2);
$multistrike[0] = number_format((float)$multistrike[0], 2, '.', '');
return $multistrike;
}
public function returnLeech($q) {
$leech[0] = round($q['leech'], 2);
$leech[1] = round($q['leechRating'], 2);
$leech[0] = number_format((float)$leech[0], 2, '.', '');
return $leech;
}
public function returnVersatility($q) {
$versa[0] = round($q['versatility'], 2);
$versa[1] = round($q['versatilityDamageDoneBonus'], 2);
$versa[2] = round($q['versatilityHealingDoneBonus'], 2);
$versa[3] = round($q['versatilityDamageTakenBonus'], 2);
$versa[1] = number_format((float)$versa[1], 2, '.', '');
$versa[2] = number_format((float)$versa[2], 2, '.', '');
$versa[3] = number_format((float)$versa[3], 2, '.', '');
return $versa;
}
public function returnAvoidance($q) {
$avoidence[0] = round($q['avoidanceRatingBonus'], 2);
$avoidence[1] = round($q['avoidanceRating'], 2);
$avoidence[0] = number_format((float)$avoidence[0], 2, '.', '');
return $avoidence;
}

private function getStatInfo($q) {
$iStats = array(
"-1" => "None",
"0" => "Mana",
"1" => "Health",
"3" => "Agility",
"4" => "Strength",
"5" => "Intellect",
"6" => "Spirit",
"7" => "Stamina",
//-------------------------------
"12" => "Defense Skill",
"13" => "Dodge",
"14" => "Parry",
"15" => "Block",
"16" => "Melee Hit",
"17" => "Ranged Hit",
"18" => "Spell Hit",
"19" => "Melee Crit",
"20" => "Ranged Crit",
"21" => "Spell Crit",
"22" => "Melee Hit Taken",
"23" => "Ranged Hit Taken",
"24" => "Spell Hit Taken",
"25" => "Melee Crit Taken",
"26" => "Ranged Crit Taken",
"27" => "Spell Crit Taken",
"28" => "Melee Haste",
"29" => "Ranged Haste",
"30" => "Spell Haste",
"31" => "Hit",
"32" => "Crit",
"33" => "Hit Taken",
"34" => "Crit Taken",
"35" => "Resilience",
"36" => "Haste",
"37" => "Expertise",
"38" => "Attack Power",
"39" => "Ranged Attack Power",
"40" => "Versatility",
"41" => "Spell Healing Done", // deprecated
"42" => "Spell Damage Done", // deprecated
"43" => "Mana Regeneration",
"44" => "Armor Penetration",
"45" => "Spell Power",
"46" => "Health Regen",
"47" => "Spell Penetration",
"48" => "Block Value",
"49" => "Mastery",
"50" => "Bonus Armor",
"51" => "Fire Resistance",
"52" => "Frost Resistance",
"53" => "Holy Resistance",
"54" => "Shadow Resistance",
"55" => "Nature Resistance",
"56" => "Arcane Resistance",
"57" => "PVP Power",
"58" => "Amplify",
"59" => "Multistrike",
"60" => "Readiness",
"61" => "Speed",
"62" => "Leech",
"63" => "Avoidence",
"64" => "Indestructible",
"65" => "WOD_5",
"66" => "Cleave",
//-------------------------------
"71" => "Strength/Agility/Intelect",
"72" => "Strength/Agility",
"73" => "Agility/Intelect",
"74" => "Strength/Intelect"
);
return $iStats[$q];
}

private function itemQuality($q) {
switch ($q) {
case 1: $quality = "#FFFFFF"; break;
case 2: $quality = "#00CC00"; break;
case 3: $quality = "#3365FF"; break;
case 4: $quality = "#8819F0"; break;
case 5: $quality = "#F18000"; break;
case 6: $quality = "#E1CC7C"; break;
case 7: $quality = "#E1CC7C"; break;
default: $quality = "#8D9B9B";
}
return $quality;
}
public function loadStats($q) {
$sItems = $this->cInfo['items'][$q];
if (!$sItems) {
$armor['name'] = "Nothing Equiped";
$armor['icon'] = "./img/empty.jpg";
$armor['quality'] = "#FFFFFF";
$armor['ilevel'] = "&nbsp;";
}
else {
$armor['id'] = $sItems['id'];
$armor['name'] = $sItems['name'];
$armor['icon'] = "http://media.blizzard.com/wow/icons/56/" . $sItems['icon'] . ".jpg";
$armor['quality'] = $this->itemQuality($sItems['quality']);
$armor['ilevel'] = "<br /><hr />Item Level " . $sItems['itemLevel'];
}
return $armor;
}
public function loadAvatar() {
$thumbN = "http://" . $this->region . ".battle.net/static-render/" . $this->region . "/" . $this->cInfo['thumbnail'];
$thumbN = str_replace("avatar", "profilemain", $thumbN);
return $thumbN;
}
}
?>
this is all based on arrays, returns. in a php class
to call upon it


$sentaPedia = new sentaPedia("us", "stormrage", "aresbale");
print_r($sentaPedia->cInfo);
Hey Ares, the forums don't have the best support for code formatting. You may want to create a gist or something to preserve the formatting of your code.
ok i will :) ty

Join the Conversation

Return to Forum