Skip to content

Instantly share code, notes, and snippets.

@jkaflik
Created July 22, 2011 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkaflik/1099330 to your computer and use it in GitHub Desktop.
Save jkaflik/1099330 to your computer and use it in GitHub Desktop.
PHP library whose gets OTS status
<?
//* Made by Kofel (kofels@gmail.com)
//* Under GPL
//* Writed in PHP5 and used SimpleXML
//well, this version is refactored
class ServerStatus
{
protected $IP, $port;
public $rawData;
public function __construct( $IP, $port = 7171 )
{
$this->IP = $IP;
$this->port = (int)$port;
}
public function update()
{
$this->rawData = "";
$socket = fsockopen( $this->IP, $this->port, $e, $ee, 2 );
if( !$socket ) return false;
fwrite( $socket, chr(6) . chr(0) . chr(255) . chr(255) . 'info' );
while( !feof( $socket ) )
{
$this->rawData .= fgets( $socket, 64 );
}
fclose( $socket );
return true;
}
public function parse()
{
if( empty( $this->rawData ) )
throw new BadMethodCallException('No raw data found. Please call update before. Make sure, that you are not doing too much requests, because OTS have to refuse often requests from same IP');
$xml=new SimpleXMLElement($this->rawData);
$tmp=array();
$tmp['serverinfo']['uptime']=(int)$xml->serverinfo->attributes()->uptime;
$tmp['serverinfo']['ip']=(string)$xml->serverinfo->attributes()->ip;
$tmp['serverinfo']['name']=(string)$xml->serverinfo->attributes()->servername;
$tmp['serverinfo']['port']=(int)$xml->serverinfo->attributes()->port;
$tmp['serverinfo']['location']=(string)$xml->serverinfo->attributes()->location;
$tmp['serverinfo']['site']=(string)$xml->serverinfo->attributes()->url;
$tmp['serverinfo']['server']=(string)$xml->serverinfo->attributes()->server;
$tmp['serverinfo']['version']=(int)$xml->serverinfo->attributes()->version;
$tmp['serverinfo']['client']=(int)$xml->serverinfo->attributes()->client;
$tmp['owner']['name']=(string)$xml->owner->attributes()->name;
$tmp['owner']['email']=(string)$xml->owner->attributes()->email;
$tmp['players']['online']=(int)$xml->players->attributes()->online;
$tmp['players']['max']=(int)$xml->players->attributes()->max;
$tmp['players']['peak']=(int)$xml->players->attributes()->peak;
$tmp['monsters']['total']=(int)$xml->monsters->attributes()->total;
$tmp['map']['name']=(string)$xml->map->attributes()->name;
$tmp['map']['author']=(string)$xml->map->attributes()->author;
$tmp['map']['width']=(int)$xml->map->attributes()->width;
$tmp['map']['height']=(int)$xml->map->attributes()->height;
$tmp['motd']=(string)$xml->motd;
return $tmp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment