send($socket); // high five $start = microtime(true); $pingPacket->send($socket); $length = self::readVarInt($socket); $ping = round((microtime(true) - $start) * 1000); // read the requested data $data = socket_read($socket, $length, PHP_NORMAL_READ); $data = strstr($data, '{'); $data = json_decode($data); $descriptionRaw = isset($data->description) ? $data->description : false; $description = $descriptionRaw; // colorize the description if it is supported if (gettype($descriptionRaw) == 'object') { $description = ''; if (isset($descriptionRaw->text)) { $color = isset($descriptionRaw->color) ? $descriptionRaw->color : ''; $description = '' . $descriptionRaw->text . ''; } if (isset($descriptionRaw->extra)) { foreach ($descriptionRaw->extra as $item) { $description .= isset($item->bold) && $item->bold ? '' : ''; $description .= isset($item->color) ? '' . $item->text . '' : ''; $description .= isset($item->bold) && $item->bold ? '' : ''; } } } return array( 'hostname' => $host, 'port' => $port, 'ping' => $ping, 'version' => isset($data->version->name) ? $data->version->name : false, 'protocol' => isset($data->version->protocol) ? $data->version->protocol : false, 'players' => isset($data->players->online) ? $data->players->online : false, 'max_players' => isset($data->players->max) ? $data->players->max : false, 'description' => $description, 'description_raw' => $descriptionRaw, 'favicon' => isset($data->favicon) ? $data->favicon : false, 'modinfo' => isset($data->modinfo) ? $data->modinfo : false ); } private static function readVarInt ($socket) { $a = 0; $b = 0; while (true) { $c = socket_read($socket, 1); if (! $c) { return 0; } $c = Ord($c); $a |= ($c & 0x7F) << $b ++ * 7; if ($b > 5) { return false; } if (($c & 0x80) != 128) { break; } } return $a; } }