Skip to content

Instantly share code, notes, and snippets.

@FT45
Created December 15, 2010 21:40
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 FT45/742655 to your computer and use it in GitHub Desktop.
Save FT45/742655 to your computer and use it in GitHub Desktop.
<?php
class srv {
public $ip;
public $port;
public $timeout;
public $socket;
public $command = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xffgief";
public $ping;
public $version;
public $name;
public $map;
public $gametype;
public $flags;
public $progression;
public $playerCount;
public $maxPlayers;
public $players = array();
public function query($ip = '', $port = '') {
if($ip == '' | $port = ''):
echo '<p><b>Es konnte keine Verbindung zum Server aufgebaut werden.</b></p>';
endif;
$this->ip = $ip;
$this->port = $port;
$this->timeout = 2;
$this->socket = stream_socket_client('udp://' . $this->ip . ':' . $this->port, $errno, $errstr, $this->timeout);
stream_set_timeout($this->socket, $this->timeout);
if (!$this->socket):
echo 'Type 1';
$time_start = microtime(true);
fwrite($this->socket, $this->command, strlen($this->command));
$response = fread($this->socket, 2048);
$time_end = microtime(true);
echo 'Type 2';
if ($response):
echo 'Type 3';
$this->ping = round(($time_end - $time_start) * 1000);
$this->parseServerResponse($response);
echo 'Type 4';
fclose($this->socket);
return true;
else:
fclose($this->socket);
return false;
endif;
else:
echo 'error';
return false;
endif;
}
private function parseServerResponse($reponse) {
echo 'Type 5';
$matches = array();
preg_match('/
info(\d\.\d\.\d) # server version
\0(.+?) # server name
\0(\w+) # map name
\0(ctf|tdm|dm|mod) # game type
\0(\d+) # flags
\0(\d+) # progression
\0(\d+) # player count
\0(\d+) # max players
\0(.*) # players
/ix', $response, $matches);
$this->version = $matches[1];
$this->name = $matches[2];
$this->map = $matches[3];
$this->gametype = $matches[4];
$this->flags = $matches[5];
$this->progression = $matches[6];
$this->playerCount = $matches[7];
$this->maxPlayers = $matches[8];
}
}
?>
<?php
$srv = new srv();
$srv->query('95.156.208.254', '8307');
echo $srv->name;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment