Skip to content

How to use the standard api over http

Insidiea edited this page Jan 29, 2012 · 7 revisions

Docs for SDK used

The PHP SDK is used in this example. Documentation is available here: http://ramblingwood.com/minecraft/jsonapi/phpsdkdocs/jsonapi/jsonapi.html

Simple demo

PHP:

<?php
require('JSONAPI.php'); // get this file at: https://github.com/alecgorge/jsonapi/raw/master/sdk/php/JSONAPI.php

$api = new JSONAPI("localhost", 20059, "username", "password", "salt");

var_dump($api->call("getPlayerLimit"));
?>

Output:

array(3) {
  ["result"]=>
  string(7) "success"
  ["source"]=>
  string(14) "getPlayerLimit"
  ["success"]=>
  int(20)
}

Call multiple methods at the same time

You can call multiple methods at the same time and get the results all at once. If you need to fetch a lot of data from the server, this can cut down the number of requests you need to make.

PHP:

<?php
require('JSONAPI.php'); // get this file at: https://github.com/alecgorge/jsonapi/raw/master/sdk/php/JSONAPI.php

$api = new JSONAPI("localhost", 20059, "username", "password", "salt");

var_dump($api->callMultiple(array(
	"getPlayerLimit",
	"getPlayerCount"
	), array(
	array(),
	array(),
)));
?>

Output:

array(3) {
  ["result"]=>
  string(7) "success"
  ["source"]=>
  array(2) {
    [0]=>
    string(14) "getPlayerLimit"
    [1]=>
    string(14) "getPlayerCount"
  }
  ["success"]=>
  array(2) {
    [0]=>
    array(3) {
      ["result"]=>
      string(7) "success"
      ["source"]=>
      string(14) "getPlayerLimit"
      ["success"]=>
      int(20)
    }
    [1]=>
    array(3) {
      ["result"]=>
      string(7) "success"
      ["source"]=>
      string(14) "getPlayerCount"
      ["success"]=>
      int(0)
    }
  }
}