Here i will give you a short explanation how to use the integrated API of the TinkertownServer GUI.
To use the API you need to have the webserver of the GUI running and the API-key specified.
Keep this key secret. Everyone who knows this key can control the server!
If you have you server running on a public server with a URL you can use the URL to get to the API.
Example: http://[YourServerURL]:[yourWebserverPort]/APIv1
If your server is only reacable via IP you can find your public ip down in the GUI. The webserver port needs to be forwarded if you are behind a router and opppend in the windows firewall.
Example: http://[YourServerIP]:[yourWebserverPort]/APIv1
The WebserverPort only needs to be set if it is not Port 80.
Backend
There is a backend for remote access via browser.
On this backend you can start/restart/stop the server, see the GUI log and see the serverstats
URL: /APIv1/[APIkey]/backend
Screenshot:
API URLs and usage examples
In the following list i will give you all possible commands and some php-codesnipppets for implementing it into a website.
[ServerURL] and [APIkey] in the examples need to be replaced with your URL and API-Key.
- /APIv1/[APIkey]/json
The response of this is the serverstatus, the playeronline and the max player in JSON-Format.
Example:{ "status": "running", "playeronline": "2", "maxplayer": "10" }
Example PHP code:
<?php $ctx = stream_context_create(array('http'=> array( 'timeout' => 1, //1200 Seconds is 20 Minutes ) )); $hp = file_get_contents("[ServerURL]/APIv1/[APIkey]/json", false, $ctx); $status = ""; $players = ""; $maxplayers = ""; $decode_data = json_decode($hp); foreach($decode_data as $key=>$value){ if($key == "status") $status = $value; if($key == "playeronline") $players = $value; if($key == "maxplayer") $maxplayers = $value; } if($status == "") { //echo "Statusupdate currently not working"; echo "Server is <font color='#CC0000'>OFFLINE</font>"; } else { if ($status == "running") { echo "The Server is <font weight='bold' color='#00CC00'>ONLINE</font><br>"; echo $players ."/".$maxplayers." playerslots used"; } else { echo "The Server is <font weight='bold' color='#CC0000'>OFFLINE</font>"; } } ?>
- /APIv1/[APIkey]/status
Response is the serverstatus in clear text (running/stopped)
Example PHP:<?php $status=file_get_contents("[ServerURL]/APIv1/[APIkey]/status"); echo $status; ?>
- /APIv1/[APIkey]/getplayers
Response is the number of currently online players in clear text
Example PHP:<?php $status=file_get_contents("[ServerURL]/APIv1/[APIkey]/getplayers"); echo $status; ?>
- /APIv1/[APIkey]/maxplayers
Response is the max. players that can connect in clear text (running/stopped)
Example PHP:<?php $status=file_get_contents("[ServerURL]/APIv1/[APIkey]/maxplayers"); echo $status; ?>
- /APIv1/[APIkey]/start
If URL is loaded the Server will be started if not running
Example PHP:<?php echo '<form method="post" action="/"> <button type="submit" name="start">start server</button> </form>'; if(isset($_POST['start'])){ $homepage = file_get_contents("[ServerURL]/APIv1/[APIkey]/start"); echo $homepage; } ?>
- /APIv1/[APIkey]/restart
If URL is loaded the Server will be restarted if running
Example PHP:<?php echo '<form method="post" action="/"> <button type="submit" name="restart">restart server</button> </form>'; if(isset($_POST['restart'])){ $homepage = file_get_contents("[ServerURL]/APIv1/[APIkey]/restart"); echo $homepage; } ?>
- /APIv1/[APIkey]/stop
If URL is loaded the Server will be stopped if running
Example PHP:<?php echo '<form method="post" action="/"> <button type="submit" name="stop">stopserver</button> </form>'; if(isset($_POST['stop'])){ $homepage = file_get_contents("[ServerURL]/APIv1/[APIkey]/stop"); echo $homepage; } ?>