2017-04-16 15:58:05 +01:00
|
|
|
<?php
|
2025-03-29 10:50:03 +00:00
|
|
|
|
2022-02-14 20:48:15 -08:00
|
|
|
/**
|
|
|
|
|
* Queries Minecraft servers using the external querying API.
|
2017-04-16 15:58:05 +01:00
|
|
|
*
|
2022-02-14 20:48:15 -08:00
|
|
|
* @package NamelessMC\Minecraft
|
|
|
|
|
* @author Samerton
|
2022-04-30 17:18:07 -07:00
|
|
|
* @version 2.0.0-pr13
|
2022-02-14 20:48:15 -08:00
|
|
|
* @license MIT
|
2017-04-16 15:58:05 +01:00
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
class ExternalMCQuery
|
|
|
|
|
{
|
2021-04-12 18:36:34 -07:00
|
|
|
/**
|
|
|
|
|
* Basic server query.
|
2021-10-30 14:52:43 +02:00
|
|
|
*
|
2024-03-09 03:19:55 -08:00
|
|
|
* @param string $ip IP to query
|
|
|
|
|
* @param int $port Port to query, `25565` by default.
|
|
|
|
|
* @param bool $bedrock Whether this is a Bedrock server or not.
|
2022-04-30 17:18:07 -07:00
|
|
|
* @return object|false Query result, false on failure.
|
2021-04-12 18:36:34 -07:00
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public static function query(string $ip, int $port = 25565, bool $bedrock = false)
|
|
|
|
|
{
|
2022-04-30 17:18:07 -07:00
|
|
|
$client = HttpClient::get('https://api.namelessmc.com/api/' . ($bedrock ? 'bedrock' : 'server') . '/' . $ip . '/' . $port);
|
|
|
|
|
|
|
|
|
|
if (!$client->hasError()) {
|
|
|
|
|
return $client->json();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2017-04-16 15:58:05 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-12 18:36:34 -07:00
|
|
|
/**
|
|
|
|
|
* Get a server's favicon.
|
2021-10-29 22:09:38 -07:00
|
|
|
*
|
2024-03-09 03:19:55 -08:00
|
|
|
* @param string $ip Server's IP.
|
|
|
|
|
* @param bool $bedrock Whether this is a Bedrock server or not.
|
2022-02-15 13:48:14 -08:00
|
|
|
* @return string|false Server's favicon, false on failure.
|
2021-04-12 18:36:34 -07:00
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public static function getFavicon(string $ip, bool $bedrock = false)
|
|
|
|
|
{
|
2022-02-15 13:48:14 -08:00
|
|
|
$query_ip = explode(':', $ip);
|
2017-04-16 15:58:05 +01:00
|
|
|
|
2022-02-15 13:48:14 -08:00
|
|
|
if (count($query_ip) !== 2 && count($query_ip) !== 1) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-04-16 15:58:05 +01:00
|
|
|
|
2022-02-15 13:48:14 -08:00
|
|
|
$ip = $query_ip[0];
|
|
|
|
|
$port = $query_ip[1] ?? ($bedrock ? 19132 : 25565);
|
2022-02-01 21:38:46 -08:00
|
|
|
|
2022-04-30 17:18:07 -07:00
|
|
|
$client = HttpClient::get('https://api.namelessmc.com/api/' . ($bedrock ? 'bedrock' : 'server') . '/' . $ip . '/' . $port);
|
|
|
|
|
if ($client->hasError()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$result = $client->json();
|
2017-06-25 20:25:08 +01:00
|
|
|
|
2022-02-15 13:48:14 -08:00
|
|
|
if (!$result->error && $result->response->description->favicon) {
|
|
|
|
|
return $result->response->description->favicon;
|
2017-06-25 20:25:08 +01:00
|
|
|
}
|
2022-02-15 13:48:14 -08:00
|
|
|
|
2017-06-25 20:25:08 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
2020-12-13 19:38:04 +01:00
|
|
|
}
|