Sep 052010
 

Runescape offers an interface for fansites to grab hiscores info without having to load the entire hiscores page and use complicated scripts to pull out each piece. You can get the info by visiting this page: http://hiscore.runescape.com/index_lite.ws?player=yourusername

The page will feed back info in line break and comma separated form. Each skill is separated by a line break and rank/level/xp are seperated by commas. In its default form it’s not very usable, you’d need to write some sort of script to format it. I was bored and decided to make a PHP script that converts the page into JSON format, making it very easy to use with a Javascript front-end.

 array(        
						"Attack", "Defence", "Strength",
						"Hitpoints", "Ranged", "Prayer",
						"Magic", "Cooking", "Woodcutting",
						"Fletching", "Fishing", "Firemaking",
						"Crafting", "Smithing", "Mining",
						"Herblore", "Agility", "Thieving",
						"Slayer", "Farming", "Runecrafting",
						"Hunter", "Construction", "Summoning",
						"Dungeoneering"
						),
				"minigames" => array(
						"Duel Tournaments", "Bounty Hunters",
						"Bounty Hunter Rogues", "Fist of Guthix",
						"Mobilising Armies", "B.A Attackers",
						"B.A Defenders", "B.A Healers",
						"Castle Wars Games", "Conquest"
				)
		);
		$name = strtolower($name);
		$connection = fopen('http://hiscore.runescape.com/index_lite.ws?player='.$name, 'rb');
		if($http_response_header[0] === 'HTTP/1.1 200 OK') {
			$data = stream_get_contents($connection);
			$data = explode("\n", $data);
			$overall = explode(',', $data[0]);
			$stats['overall'] = array("Rank" => $overall[0], "Level" => $overall[1], "XP" => $overall[2]);
			for($i = 0; $i < 25; $i++) {
				$cItem = $hiscores['stats'][$i];
				$num = explode(',', $data[$i+1]);
				$stats['skills'][$cItem] = array("Rank" => $num[0], "Level" => $num[1], "XP" => $num[2]);
			}
			for($i = 0; $i < 9; $i++){
				$cItem = $hiscores['minigames'][$i];
				$num = explode(',', $data[$i+27]);
				$stats['minigames'][$cItem] = array("Rank" => $num[0], "Score" => $num[1]);
			}
			
			$stats = json_encode($stats);
			$stats = str_replace('-1', '', $stats);
			if($_GET['lowercase'] === 'yes'){
				$stats = strtolower($stats);
			}
			return $stats;
		}
		else {
			return '{"error":"User not found"}';
		}
		fclose($connection);
}

if(isset($_GET['user'])) {
	$cStats = getStats($user);
	if(isset($callback)) {
		echo $callback.'('.$cStats.')';
	}
	else
	{
		echo $cStats;
	}
}
else
{
	echo 'Usage:
    '; echo '
  • user=username
  • '; echo '
  • Optional: callback=funcName
  • '; echo '
  • Optional: lowercase=yes (all json will be lower case)

'; echo 'Callback function is used for cross-domain AJAX requests'; } ?>

You can access the script here or download it here. For usage information visit the script page.
Old School Runescape version available here

JSON Formatter for Runescape Hiscores by pceasies is licensed under a Creative Commons Attribution-Share-Alike 3.0 Unported License

  5 Responses to “Runescape Hiscore API in JSON Format”

  1. Do they have API for item DB ? If not how the item DB sites get the price and stats for items ?

  2. I believe fan sites just make bots that scrape the regular pages and pull the information out. The hiscores API is the only one I know of. You might be able to find a fansite with an API to get info though.

  3. Hey would you know how to grab the OSRS HS API?

  4. Very nice man. I’m making a calculator to show how much xp to get maxed total lvl using your api.