Simple Runescape highscores json callback output

I wrote this for a Runescape highscore plugin I had been working on. It allows me to grab the Runescape lite highscores and output it in a JSON callback format. Special thanks for http://recursive-design.com/blog/2008/03/11/format-json-with-php/ for writing a pretty JSON indent method as the current version of PHP on this web server does not support JSON_PRETTY_PRINT.

<?php
header('Content-type: application/javascript');
$player = isset($_GET['player']) ? $_GET['player'] :"zezima";
$callback = isset($_GET['callback']) ? $_GET['callback'] :"runescape.parseHighscores";
const TOTAL_ACTIVITIES = 38;
const TOTAL_SKILLS = 25;
 
    $name = array("Overall","Attack","Defence","Strength","Constitution","Ranged","Prayer","Magic","Cooking","Woodcutting","Fletching","Fishing","Firemaking","Crafting","Smithing","Mining","Herblore","Agility","Thieving","Slayer","Farming","Runecrafting","Hunter","Construction","Summoning","Dungeoneering","Duel Tournament","Bounty Hunters","Bounty Hunter Rogues","Fist of Guthix","Mobilising Armies","B.A Attackers","B.A Defenders","B.A Collectors","B.A Healers","Castle Wars Games","Conquest","Dominion Tower");
    $url ='http://hiscore.runescape.com/index_lite.ws?player='.$_GET['player'];
    $process = curl_init($url);
    curl_setopt($process, CURLOPT_HEADER, 0);
    curl_setopt($process, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($process,CURLOPT_CONNECTTIMEOUT,1);
    $resp = curl_exec($process);
    $resp = explode("\n", $resp);
    curl_close($process);
    for($i = 0; $i < 38; $i++){
    $s = explode( "," , $resp[$i]);
        $skillData[$i] = $i < TOTAL_SKILLS ? array("id" =>(int)$i,"isSkill" => (boolean)true,"name" => $name[$i],"rank" => (int)$s[0],"level" => (int)$s[1],"experience" => (int)$s[2]) : array("id" =>(int)$i,"isSkill" => (boolean)false,"name" => $name[$i],"rank" => (int)$s[0],"score" => (int)$s[1]);
    }
 
   $data = json_encode($skillData);// because my host sucks and does not use PHP 5.4 so I can't use the FANCY JSON OUTPUT so I have to use the indent method for debugging.
   echo $callback .'({'."\n  ".'"data":'. indent($data).'})';
 
 
 
 
 //Method from: http://recursive-design.com/blog/2008/03/11/format-json-with-php/
 function indent($json) {
 
    $result      = '';
    $pos         = 0;
    $strLen      = strlen($json);
    $indentStr   = '  ';
    $newLine     = "\n";
    $prevChar    = '';
    $outOfQuotes = true;
 
    for ($i=0; $i<=$strLen; $i++) {
 
        // Grab the next character in the string.
        $char = substr($json, $i, 1);
 
        // Are we inside a quoted string?
        if ($char == '"' && $prevChar != '\\') {
            $outOfQuotes = !$outOfQuotes;
 
        // If this character is the end of an element,
        // output a new line and indent the next line.
        } else if(($char == '}' || $char == ']') && $outOfQuotes) {
            $result .= $newLine;
            $pos --;
            for ($j=0; $j<$pos; $j++) {
                $result .= $indentStr;
            }
        }
 
        // Add the character to the result string.
        $result .= $char;
 
        // If the last character was the beginning of an element,
        // output a new line and indent the next line.
        if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) {
            $result .= $newLine;
            if ($char == '{' || $char == '[') {
                $pos ++;
            }
 
            for ($j = 0; $j < $pos; $j++) {
                $result .= $indentStr;
            }
        }
 
        $prevChar = $char;
    }
 
    return $result;
}
 
?>

Example output: http://silabsoft.org/rs-web/highscore.php?player=zezima

GitHub:https://github.com/silabsoft/rs-web

3 Responses to Simple Runescape highscores json callback output

  1. HcoJustin says:

    Jagex updated the highscores a little bit, new name array should be

    $name = array("Overall","Attack","Defence","Strength","Constitution","Ranged","Prayer","Magic","Cooking","Woodcutting","Fletching","Fishing","Firemaking","Crafting","Smithing","Mining","Herblore","Agility","Thieving","Slayer","Farming","Runecrafting","Hunter","Construction","Summoning","Dungeoneering","Jaimy Hunters","Jaimy Hunter Rogues","Dominion Tower","The Crucible","Castle Wars Games", "B.A Attackers", "B.A Defenders","B.A Collectors","B.A Healers","Duel Tournament","Mobilising Armies","Conquest", "Fist of Guthix", "G.G Resource Race", "G.G Athletics");

Leave a Reply

*



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">