• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[ZNOTE] API

raf

Active Member
Joined
Jan 10, 2011
Messages
261
Reaction score
38
Location
Warsaw, PL
Could someone please point me the way, or just explain, how to use ZnoteAAC API ? I'm trying to get player information in json. But i have no idea how to correctly use that API.
 
i managed to see and try to edit modules/samples/blank.php file, it works. But when i try to use topExperience.php file, it's showing error 500.

I've tried to do it this way, no success:

PHP:
$playername = getValue($_GET['name']);
if ($playername !== false) {
    $player = mysql_select_single("SELECT `name` FROM `players` WHERE `name`='$playername';");
    $response['player']['name'] = $player;
}
 
Last edited:
Are you sending the response (SendResponse($response))? The return of mysql_select_single is an array, even if you only fetch one field. The player name is in $player['name'].

Edit: I'm a bit intrigued with the API myself, so I wrote a simple test module to fetch player name and level.
It looks like this:
Code:
<?php require_once '../../module.php';
// Configure module version number
$response['version']['module'] = 1;

$playername = getValue($_GET['name']);
$player = mysql_select_single("SELECT `name`, `level` FROM `players` WHERE `name` = '$playername';");
if ($player !== false) {
    $response['data']['player'] = $player;
} else {
    // If no player is found, we send an error message
    $response['error']['message'] = 'Could not find a player with that name.';
}

// Send the response through JSON API
SendResponse($response);
?>

Response:
Code:
// Player exixsts
{"version":{"znote":"1.5_SVN","ot":"TFS_10","module":1},"data":{"player":{"name":"Forgee","level":"8"}}}
// Player doesn't exixst
{"version":{"znote":"1.5_SVN","ot":"TFS_10","module":1},"error":{"message":"Could not find a player with that name."}}
To parse the response back into an array in PHP you would use json_parse. For ajax you can set the dataType as json and it will return a JS object.

It could probably do with some toughening up to prevent excessive SQL load, perhaps cache players and check if the player has been loaded recently (is in cache) before running the query. Maybe someone will find it useful as an example.
 
Last edited:
Are you sending the response (SendResponse($response))? The return of mysql_select_single is an array, even if you only fetch one field. The player name is in $player['name'].
Yes i did. I think it's outdated or not 100% working API, because there is player class to fetch info about players, but it doesn't seem to work.

I've wrote player fetching too.

PHP:
<?php
require_once 'engine/init.php';
protect_page();
admin_only($user_data);
session_start();
ob_start();

$sessionPrefix = $config['session_prefix'];
if (isset($_SESSION['token'])) {
  $_SESSION['old_token'] = $_SESSION['token'];
}
Token::generate();

function SendResponse($response) {
  echo json_encode($response);
}

$playername = $_GET['name'];
$user_id = user_character_exist($playername);
if ($user_id !== false) {
  $data = mysql_select_single("SELECT * FROM `players` WHERE `name` LIKE '$playername'");
  $json['data']['player'] = $data;
  $json['data']['player']['vocation'] = vocation_id_to_name($data['vocation']);
}
SendResponse($json);
?>

Do you call your module like this ?

Code:
http://domain.name/api/modules/player/player.php?name=Raff
 
Do you call your module like this ?

Code:
http://domain.name/api/modules/player/player.php?name=Raff
Yes I do.

You should not need to set up the session, have a look at api/api.php, it handles that. It also includes config.php, engine/function/users.php, general.php, database/connect.php. https://github.com/Znote/ZnoteAAC/blob/master/api/api.php#L9-L12
Just include module.php in your script and you should be good.
Code:
require_once '../../module.php';
I'm a bit curious as to why it doesn't just include init.php, as we loose out on the "spam protection" and user data. I suppose the api is intended to be publicly accessible and he wanted to keep it as light as possible, but I don't know.

I will probably experiment with it a bit more later.
 
Alright, that helps me already :) I hope Znote will come back to developing this AAC. And improve some stuff! If i do something cool with API, i will post it here. Thank you
 
I suppose the api is intended to be publicly accessible and he wanted to keep it as light as possible, but I don't know.

This is correct, I originally got the idea of creating a json api for the purpose of letting third party services access OT related data. For instance if otservlist wanted to fetch the highscores and show it on their site.
But then I got a bit over ambitious and wanted to make a single page application template with etc Angularjs. This requires that I make lots of modules for all kinds of ajax calls. I got off to a nice start preparing to create a player class but reality kicked in and well.. RIP spare time. I am still dreaming of completing it, but who knows if /& when I'll have time to do that.

Tip: If you want to read the response easier, add debug as a get parameter in the url. Etc:
http://demo.znote.eu/api/modules/highscores/topExperience.php?debug
 
Back
Top