Easy HowTo
the simplay way to query a game
OpenTTDLib::queryServer()
This is the easiest way to query a server. The queryServer() method of OpenTTDLib does everything for you and will either return true on success or throws an Exception on any kind of error.
Details of the Exception thrown can be retrieved with OpenTTDLibException::getMessage().
OpenTTDLibException::getTrace() can be used for greater detail. For reporting occured Exceptions, please add the output of OpenTTDLibException::dumpForDebug().
Note: before v0.2 or r24 OpenTTDLib would return false rather than throwing an exception.
Please refer to the class documentation of OpenTTDLib::queryServer() for the exact syntax of this method.
Examples
Minmal way of querying a server (as of r24 or release:OpenTTDLib-0.1.2):
<?php
require_once( 'includes/OpenTTDLibException.php' );
require_once( 'includes/OpenTTDLibPacket.php' );
require_once( 'includes/OpenTTDLibCache.php' );
require_once( 'includes/OpenTTDLib.php' );
$openttd = new OpenTTDLib();
try{
$openttd->queryServer();
$server_info = $openttd->getInfo();
$server_detail = $openttd->getDetail();
}
catch( OpenTTDLibException $e ){
die( $e->getTrace() );
}
?>
If no parameters are passed to OpenTTD::queryServer() the default address queried is
localhost:3979 requesting all supported details, with a timeout of 5 seconds.
Passing optional parameters like host, port and an options array
<?php
require_once( 'includes/OpenTTDLibException.php' );
require_once( 'includes/OpenTTDLibPacket.php' );
require_once( 'includes/OpenTTDLibCache.php' );
require_once( 'includes/OpenTTDLib.php' );
$openttd = new OpenTTDLib();
$options = array(
OpenTTDLib::OPTION_QUERY_TYPE => OpenTTDLib::QUERYTYPE_INFO,
OpenTTDLib::OPTION_TIMEOUT => 15,
);
try{
$openttd->queryServer( 'localhost', 27016, $options );
$server_info = $openttd->getInfo();
}
catch( OpenTTDLibException $e ){
die( $e->getTrace() );
}
?>
this will query localhost:27016, only fetching $info and using a timeout of 15 seconds.