/** * Read and parse the response * * @param resource $fp Handle to an open socket * @throws \Exception */ private function readResponse($fp) { $res = fread($fp, 3); if (strpos($res, 0xff) !== 0) { throw new \Exception('Invalid ping response'); } $res = fread($fp, 2048); $data = explode(pack('n', 0), $res); $this->gs->setName(self::decodeUTF16BEString($data[3])); $this->gs->setPlayerCount((int) self::decodeUTF16BEString($data[4])); $this->gs->setMaxPlayers((int) self::decodeUTF16BEString($data[5])); }
/** * Parse the JSON string and set server status properties * * @param string $jsonString * @throws \Exception */ private function parseJSON($jsonString) { $json = json_decode($jsonString); if (!$json) { throw new \Exception('Invalid JSON string'); } $name = $json->description; $this->gs->setName(is_string($name) ? $name : $name->text); $this->gs->setPlayerCount($json->players->online); $this->gs->setMaxPlayers($json->players->max); if (property_exists($json->players, 'sample')) { $players = array(); foreach ($json->players->sample as $player) { $players[] = $player->name; } $this->gs->setPlayerList($players); } }
/** * Parse the players section of the response * * @param resource $fp Handle to an open socket */ private function parsePlayers($fp) { $players = array(); $val = ''; while (true) { $res = fread($fp, 1); if ($res === "" || $res === false) { if (strlen($val) === 0) { break; } else { $players[] = $val; $val = ''; } } else { $val .= $res; } } $this->gs->setPlayerList($players); }