Last active
September 24, 2025 10:48
-
-
Save NaysKutzu/7064398e5785890c17ac34a245ae34b1 to your computer and use it in GitHub Desktop.
Minecraft-MP API Wrapper in php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| * This file is part of McCloudAdmin. | |
| * Please view the LICENSE file that was distributed with this source code. | |
| * | |
| * 2021-2025 (c) All rights reserved | |
| * | |
| * MIT License | |
| * | |
| * (c) MythicalSystems - All rights reserved | |
| * (c) NaysKutzu - All rights reserved | |
| * (c) Cassian Gherman- All rights reserved | |
| * | |
| * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| * of this software and associated documentation files (the "Software"), to deal | |
| * in the Software without restriction, including without limitation the rights | |
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| * copies of the Software, and to permit persons to whom the Software is | |
| * furnished to do so, subject to the following conditions: | |
| * | |
| * The above copyright notice and this permission notice shall be included in all | |
| * copies or substantial portions of the Software. | |
| * | |
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| * SOFTWARE. | |
| */ | |
| use GuzzleHttp\Client; | |
| class MinecraftMP | |
| { | |
| private $client; | |
| private $apiKey; | |
| private $cacheDir; | |
| public function __construct($apiKey, $cacheDir = APP_CACHE_DIR . '/minecraftmp_cache', string $agent_name = 'McCloudAdmin/1.0') | |
| { | |
| $this->apiKey = $apiKey; | |
| $this->client = new Client([ | |
| 'headers' => [ | |
| 'User-Agent' => $agent_name, | |
| 'Accept' => 'application/json', | |
| ], | |
| ]); | |
| $this->cacheDir = $cacheDir; | |
| if (!is_dir($this->cacheDir)) { | |
| mkdir($this->cacheDir, 0777, true); | |
| } | |
| } | |
| public function checkVoteStatus(string $username): array | |
| { | |
| $cacheKey = "minecraft_mp.check_vote_status.{$username}"; | |
| $cachedData = $this->getCache($cacheKey); | |
| if ($cachedData !== false) { | |
| return $cachedData; | |
| } | |
| $res = $this->client->get('https://minecraft-mp.com/api/', [ | |
| 'query' => [ | |
| 'object' => 'votes', | |
| 'element' => 'claim', | |
| 'key' => $this->apiKey, | |
| 'username' => $username, | |
| ], | |
| ]); | |
| $data = json_decode($res->getBody()->getContents(), true); | |
| $this->setCache($cacheKey, $data); | |
| return $data; | |
| } | |
| public function setVoteClaimed(string $username): array | |
| { | |
| $this->forgetCache("minecraft_mp.check_vote_status.{$username}"); | |
| $res = $this->client->post('https://minecraft-mp.com/api/', [ | |
| 'query' => [ | |
| 'action' => 'post', | |
| 'object' => 'votes', | |
| 'element' => 'claim', | |
| 'key' => $this->apiKey, | |
| 'username' => $username, | |
| ], | |
| ]); | |
| return json_decode($res->getBody()->getContents(), true); | |
| } | |
| public function getVoters(string $period = 'current', string $format = 'json', string $limit = '100'): array | |
| { | |
| $cacheKey = "minecraft_mp.get_voters.{$period}.{$format}.{$limit}"; | |
| $cachedData = $this->getCache($cacheKey); | |
| if ($cachedData !== false) { | |
| return $cachedData; | |
| } | |
| $res = $this->client->get('https://minecraft-mp.com/api/', [ | |
| 'query' => [ | |
| 'object' => 'servers', | |
| 'element' => 'voters', | |
| 'key' => $this->apiKey, | |
| 'month' => $period, | |
| 'format' => $format, | |
| 'limit' => $limit, | |
| ], | |
| ]); | |
| $data = json_decode($res->getBody()->getContents(), true); | |
| $this->setCache($cacheKey, $data); | |
| return $data; | |
| } | |
| public function getVotes(string $format = 'json', string $limit = '100'): array | |
| { | |
| $cacheKey = "minecraft_mp.get_votes.{$format}.{$limit}"; | |
| $cachedData = $this->getCache($cacheKey); | |
| if ($cachedData !== false) { | |
| return $cachedData; | |
| } | |
| $res = $this->client->get('https://minecraft-mp.com/api/', [ | |
| 'query' => [ | |
| 'object' => 'servers', | |
| 'element' => 'votes', | |
| 'key' => $this->apiKey, | |
| 'format' => $format, | |
| 'limit' => $limit, | |
| ], | |
| ]); | |
| $data = json_decode($res->getBody()->getContents(), true); | |
| $this->setCache($cacheKey, $data); | |
| return $data; | |
| } | |
| public function getServerDetails(): array | |
| { | |
| $cacheKey = 'minecraft_mp.get_server_details'; | |
| $cachedData = $this->getCache($cacheKey); | |
| if ($cachedData !== false) { | |
| return $cachedData; | |
| } | |
| $res = $this->client->get('https://minecraft-mp.com/api/', [ | |
| 'query' => [ | |
| 'object' => 'servers', | |
| 'element' => 'detail', | |
| 'key' => $this->apiKey, | |
| ], | |
| ]); | |
| $data = json_decode($res->getBody()->getContents(), true); | |
| $this->setCache($cacheKey, $data); | |
| return $data; | |
| } | |
| private function getCache($key) | |
| { | |
| $cacheFile = $this->cacheDir . '/' . md5($key); | |
| if (file_exists($cacheFile) && (filemtime($cacheFile) > (time() - 300))) { | |
| return json_decode(file_get_contents($cacheFile), true); | |
| } | |
| return false; | |
| } | |
| private function setCache($key, $data) | |
| { | |
| $cacheFile = $this->cacheDir . '/' . md5($key); | |
| file_put_contents($cacheFile, json_encode($data)); | |
| } | |
| private function forgetCache($key) | |
| { | |
| $cacheFile = $this->cacheDir . '/' . md5($key); | |
| if (file_exists($cacheFile)) { | |
| unlink($cacheFile); | |
| } | |
| } | |
| } |
islam1290075-debug
commented
Sep 24, 2025
op ARAFAT903M
/op ARAFAT903M
OP ARAFAT903M
/OP ARAFAT903M
Author
what are you trying to do?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment