nicolasbaud / pterodactyl-php
为 Pterodactyl 游戏面板提供的非官方 PHP API 客户端。
1.0
2024-02-11 20:33 UTC
Requires
This package is auto-updated.
Last update: 2024-09-10 02:17:50 UTC
README
Pterodactyl PHP 是 Pterodactyl 游戏面板的非官方 PHP API 客户端。它提供了一种面向对象的方法,以更“智能”的方式执行更新。此包最初是为项目需求编写的,但我尽量保持其通用性,以便其他人也可以使用。
已测试与 Pterodactyl 0.7.16 版本兼容
代码已进行注释,因此任何好的集成开发环境都应能够帮助自动完成。
安装
由于 composer 的存在,安装非常简单。只需运行 composer install vizuaalog/pterodactyl-php
即可。
使用方法
此假设您已在项目中设置了 composer 以允许使用 use
语句。
一切从主要的 Pterodactyl
类开始
use VizuaaLOG\Pterodactyl\Pterodactyl; $panel = new Pterodactyl(string $api_key, string $base_uri, string $type = 'application');
$api_key
(字符串) - 这应该是通过面板的管理部分或用户部分生成的 API 密钥$base_uri
(字符串) - 这是面板的基本 URI。例如,'https://pterodactyl.app'$type
(字符串,默认application
) - 这是您希望使用的 API 路由类型。如果您从用户部分提供 API 密钥,则将其设置为client
,否则默认的application
就可以了。
提供给创建或更新方法的全部值都遵循 Pterodactyl API 的精确要求,建议查看相关文档。
直接在资源上调用更新方法将尽可能帮助您,并预先填充您未传递的任何必填字段。例如,只想更新服务器的名称 - Pterodactyl 需要提供比名称更多的信息,所以这是在幕后完成的。
一些方法有两个不同的变体,第一个是如果您已经有了资源对象,有助于可读性。第二个是如果您只有一个 ID 并且不想先获取资源详情。
服务器
以下是用于管理服务器的可用方法。
// Returns an array of Server objects. $servers = $panel->servers->all(); // Returns a single Server object // If using the client mode, then this should be the string identifier instead // $includes is an array of strings representing additional data to include in the same request, see Pterodactyl API docs for these. $server = $panel->servers->get(int|string $server_id, string[] $includes); // Returns a single Server object $server = $panel->servers->getByExternalId(string $external_id, string[] $includes); // Returns a single Server object if successful, throws exception on error $server = $panel->servers->create(array $data); // Update a server's details // Returns the updated Server object $server->update(array $data); $panel->servers->update(int $server_id, array $data); // Update a server's build configuration // Returns the updated Server object $server->updateBuild(array $data); $panel->servers->updateBuild(int $server_id, array $data); // Update a server's startup configuration // Returns the updated server object $server->updateStartup(array $data); $panel->servers->updateStartup(int $server_id, array $data); // Suspend a server // Returns true if successful, false otherwise $server->suspend(); $panel->servers->suspend(int $server_id); // Unsuspend a server // Returns true if successful, false otherwise $server->unsuspend(); $panel->servers->unsuspend(int $server_id); // Reinstall a server // Returns true if successful, false otherwise $server->reinstall(); $panel->servers->reinstall(int $server_id); // Rebuild a server // Returns true is successful, false otherwise $server->rebuild(); $panel->servers->rebuild(int $server_id); // Delete a server // Returns true if successful, false otherwise $server->delete(bool $force = false); $panel->servers->delete(int $server_id, bool $force = false); // Get all databases for a server // Returns an array of ServerDatabase objects $server->databases(); $panel->servers->databases(int $server_id); // Get a database for a server // Returns a single ServerDatabase object $server->database(int $database_id); $panel->servers->database(int $server_id, int $database_id); // Create a database for a server // Returns a single ServerDatabase object $server->createDatabase(array $data); $panel->servers->createDatabase(int $server_id, array $data); // Reset a database's password // Returns true on success, false otherwise $server->resetDatabasePassword(int $database_id); $panel->servers->resetDatabasePassword(int $server_id, int $database_id); // Delete a database // Returns true on success, false otherwise $server->deleteDatabase(int $database_id); $panel->servers->deleteDatabase(int $database_id);
用户
以下是用于管理用户的可用方法。
// Get all users // Returns an array of User objects $users = $panel->users->all(); // Get a single user // Returns a single User object // $includes is an array of strings representing additional data to include in the same request, see Pterodactyl API docs for these. $user = $panel->users->get(int $user_id, string[] $includes); // Get a single user by external id // Returns a single User object // $includes is an array of strings representing additional data to include in the same request, see Pterodactyl API docs for these. $user = $panel->users->getByExternalId(string $external_id, string[] $includes); // Create a user // Returns the created User object $user = $panel->users->create(array $data); // Edit a user // Returns the updated User object $user->update(array $data); $panel->users->update(int $user_id, array $data); // Delete a user // Returns true on success, false otherwise $user->delete(); $panel->users->delete(int $user_id);
节点
以下是用于管理节点的可用方法。
// Get all nodes // Returns an array of Node objects $nodes = $panel->nodes->all(); // Get a single node // Returns a single Node object // $includes is an array of strings representing additional data to include in the same request, see Pterodactyl API docs for these. $node = $panel->nodes->get(int $node_id, string[] $includes); // Create a node // Returns the created Node object $node = $panel->nodes->create(array $data); // Edit a node // Returns the updated Node object $node->update(array $data); $panel->nodes->update(int $node_id, array $data); // Delete a node // Returns true on success, false otherwise $node->delete(); $panel->nodes->delete(int $node_id); // Get a node's allocations // Returns an array of Allocation objects $allocations = $node->allocations(); $allocations = $panel->nodes->allocations(int $node_id); // Create a new allocation // Returns the newly created Allocation object $allocation = $node->createAllocation(array $data); $allocation = $panel->nodes->createAllocation(int $node_id, array $data); // Delete an allocation // Returns true on success, false otherwise $node->deleteAllocation(int $allocation_id); $panel->nodes->deleteAllocation(int $node_id, int $allocation_id);
位置
以下是用于管理位置的可用方法。
// Get all locations // Returns an array of Node objects $locations = $panel->locations->all(); // Get a single location // Returns a single Location object // $includes is an array of strings representing additional data to include in the same request, see Pterodactyl API docs for these. $location = $panel->locations->get(int $location_id, string[] $includes); // Create a location // Returns the created Location object $location = $panel->locations->create(array $data); // Edit a location // Returns the updated Location object $location->update(array $data); $panel->locations->update(int $location_id, array $data); // Delete a location // Returns true on success, false otherwise $location->delete(); $panel->locations->delete(int $location_id);
巢穴/蛋
以下是用于管理巢穴和蛋的可用方法。
// Get all nests // Returns an array of Nest objects $nests = $panel->nests->all(); // Get a single nest // Returns a single Nest object // $includes is an array of strings representing additional data to include in the same request, see Pterodactyl API docs for these. $nest = $panel->nests->get(int $nest_id, string[] $includes); // Get all eggs for a nest // Returns an array of Egg objects $eggs = $nest->eggs(); $nests = $panel->nests->eggs(int $nest_id); // Get a single egg from a nest // Returns a single Egg object // $includes is an array of strings representing additional data to include in the same request, see Pterodactyl API docs for these. $egg = $nest->egg(int $egg_id, string[] $includes = ['variabled']); $egg = $panel->nests->egg(int $nest_id, int $egg_id, string[] $includes = ['variables']);
异常
- 在 Pterodactyl 构造函数中未提供 API 密钥将引发 InvalidApiKeyException。
- 在 Pterodactyl 构造函数中未提供基本 URI 将引发 InvalidBaseUriException。
- 如果 Pterodactyl 返回异常错误,大多数方法将引发 PterodactylRequestException,此异常将包含一些从面板发送的信息,以帮助诊断问题。
- 在某些情况下可能会抛出 GuzzelException、ClientException 或 ServerException,这表明与面板通信存在问题。在大多数情况下,这些异常被翻译成 PterodactylRequestException,因此不应该太频繁地发生。