Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* (c) deadinat0r */
- class m2mpQuery {
- protected $sIP, $sPort;
- private $sSocket, $sPing = 0, $sOnline = false;
- /* returns ping if connected, otherwise false */
- public function __construct($ip, $port = 27015, $timeout = 2) {
- $this->sIP = $ip;
- $this->sPort = (int)$port+1;
- $this->sSocket = @fsockopen('udp://'.$this->sIP, $this->sPort, $erNo, $erStr);
- if($this->sSocket) {
- $this->sOnline = true;
- socket_set_timeout($this->sSocket, $timeout);
- return $this->getPing();
- }
- return false;
- }
- public function __destruct() {
- @fclose($this->sSocket);
- }
- public function isOnline() {
- return $this->sOnline;
- }
- /* get players, use foreach for listing (see example) */
- public function getPlayers() {
- $r = $this->sendPacket('M2MPl');
- $r = str_replace('M2MPl@', '', $r);
- $r = str_replace($this->getInfo('players').'@', '', $r); // not the best solution, but works
- $i = explode('|', $r);
- $return = array();
- if(sizeof($i) > 0) {
- $maxPlayers = $this->getInfo('maxplayers');
- for($x = 0; $x < $maxPlayers; $x++) {
- if(@$i[$x]) {
- $ex = explode('/', $i[$x]);
- $return[] = array('id' => $ex[0], 'name' => $ex[1], 'ping' => $ex[2]);
- }
- }
- }
- return $return;
- }
- /* possible infos:
- 'name' => servername
- 'players' => plaayercount
- 'maxplayers' => maxplayercount
- 'gamemode' => gamemode name
- 'password' => password (yes = 1/no = 0)
- */
- public function getInfo($info = '') {
- $r = $this->sendPacket('M2MPi');
- if(!$r) return false;
- $r = str_replace('M2MPi@', '', $r);
- $i = explode('@', $r);
- $return = array('name' => $i[0], 'players' => $i[1], 'maxplayers' => $i[2], 'gamemode' => $i[3], 'password' => $i[4]);
- return ($info === '') ? ($return) : ($return[$info]);
- }
- /* get ping */
- public function getPing() {
- if($this->sPing === 0) {
- $start = microtime(true);
- $this->sendPacket('M2MPp');
- $this->sPing = round((microtime(true)-$start) * 1000, 0);
- }
- return $this->sPing;
- }
- /* used for sending packets, you can send custom packets directly from your object */
- public function sendPacket($packet) {
- fputs($this->sSocket, $packet);
- $r = fread($this->sSocket, 1024);
- if(!@$r || @$r === '') return false;
- else return $r;
- }
- }
- echo '<h3>Some Infos (Server: 176.65.141.8)</h3>';
- $query = new m2mpQuery('176.65.141.8'); // Testserver
- if($query !== false) {
- echo 'The server is ';
- if(!$query->isOnline()) echo 'offline';
- else {
- echo 'online';
- echo '<br />';
- $info = $query->getInfo();
- echo 'Servername: '.$info['name'];
- echo '<br />';
- echo 'Gamemode: '.$query->getInfo('gamemode');
- echo '<br />';
- echo 'Ping: '.$query->getPing();
- echo '<br /><br />';
- echo '<h3>Players</h3>';
- if($info['players'] > 0) {
- $players = $query ->getPlayers();
- echo '<table><thead><tr><td>ID</td><td>Name</td><td>Ping</td></thead><tbody>';
- foreach($players as $p) echo '<tr><td>'.$p['id'].'</td><td>'.$p['name'].'</td><td>'.$p['ping'].'</td></tr>';
- echo '</table></tbody>';
- }
- else echo 'No players online.';
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement