vizuaalog/pterodactyl-api

此包已被废弃,不再维护。作者建议使用vizuaalog/pterodactyl-php包。

Pterodactyl游戏面板的不官方PHP API客户端。

1.0.3 2020-01-01 18:10 UTC

README

Pterodactyl PHP是Pterodactyl游戏面板的不官方PHP API客户端。它提供了一种面向对象的方法,以更智能的方式执行更新。此包主要为了满足项目需求而编写,但我尽量使其尽可能通用,以便其他人也能使用。

此版本已与Pterodactyl 0.7.16进行过测试

代码已进行文档化,任何好的IDE都应能提供自动补全功能。

安装

由于使用了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,因此不应该太频繁发生。