aternos / modrinth-api
Modrinth API 的 PHP 客户端。此客户端基于 openapi 规范。
Requires
- php: ^8.1
- ext-curl: *
- ext-json: *
- ext-mbstring: *
- guzzlehttp/guzzle: ^7.3
- guzzlehttp/psr7: ^1.7 || ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.5
- phpunit/phpunit: ^10.2
README
用 PHP 编写的 Modrinth API 客户端。此客户端是 OpenAPI 生成器生成的代码和一些围绕它的包装器的组合,以改善可用性。
生成的代码位于 lib/Api
和 lib/Model
中。建议使用 lib/Client
中的包装器而不是生成的代码。
安装
通过 composer 安装包
composer require aternos/modrinth-api
用法
API 的主要入口点是 ModrinthAPIClient
类。
<?php use Aternos\ModrinthApi\Client\ModrinthAPIClient; // create an API client. This is the main entry point for the API $modrinthClient = new ModrinthAPIClient(); // set a user agent (recommended) $modrinthClient->setUserAgent('aternos/php-modrinth-api-example'); // set an api token (optional) $modrinthClient->setApiToken("api-token");
API 令牌仅适用于非公开请求,但如果提供了,它将用于所有请求。
分页列表
某些方法返回分页列表,其中包含当前页的结果列表以及导航到下一页和上一页的方法。分页列表实现了 Iterator
、ArrayAccess
和 Countable
,因此您可以使用它像数组一样。它还有一个 getResults()
方法,该方法返回基本结果数组。
搜索项目
$projects = $modrinthClient->searchProjects(); foreach ($project as $project) { // like most other methods, this method returns a wrapper // you can use the getData() method to get the project data echo $project->getData()->getTitle() . PHP_EOL; } $projects = $projects->getNextPage(); foreach ($projects as $project) { echo $project->getData()->getTitle() . PHP_EOL; }
通过选项搜索项目
您可以在搜索项目时应用过滤器和更改排序顺序。所有选项都是可选的,并且可以组合使用。
use \Aternos\ModrinthApi\Client\Options\ProjectSearchOptions; use \Aternos\ModrinthApi\Client\Options\SearchIndex; $options = new ProjectSearchOptions(); $options->setQuery("mclogs"); $options->setSearchIndex(SearchIndex::UPDATED); $projects = $modrinthClient->getProjects($options);
分面
过滤搜索结果的一种方法是通过分面。分面指定要过滤的加载器、类别、游戏版本或许可证。您需要提供一个精确的 AND 组,该组可以由多个 OR 组组成
use \Aternos\ModrinthApi\Client\Options\Facets\Facet; use \Aternos\ModrinthApi\Client\Options\Facets\FacetType; use \Aternos\ModrinthApi\Client\Options\Facets\FacetORGroup; use \Aternos\ModrinthApi\Client\Options\Facets\FacetANDGroup; $facetOrGroup = new FacetORGroup(); $facetOrGroup->addFacet(new Facet(FacetType::VERSIONS, "1.20.1")) ->addFacet(new Facet(FacetType::VERSIONS, "1.20")); // or add multiple facets at once: $facetOrGroup->addFacets( new Facet(FacetType::VERSIONS, "1.19.4"), new Facet(FacetType::VERSIONS, "1.19.3") ); // alternatively if all facets have the same type: $facetOrGroup->addFacets(FacetType::VERSIONS, "1.19.2", "1.19.1", "1.19"); $options->setFacets($facetOrGroup); // if you want to combine multiple OR groups, you can use an AND group: $facetAndGroup = new FacetANDGroup(); $facetAndGroup->addORGroup($facetOrGroup) ->addORGroup(new FacetORGroup(new Facet(FacetType::LICENSE, "MIT"))); // or $facetAndGroup = $facetOrGroup->toANDGroup() ->addORGroup(new FacetORGroup(new Facet(FacetType::LICENSE, "MIT"))); $options->setFacets($facetAndGroup); $projects = $modrinthClient->getProjects($options);
默认情况下,使用等于运算符检查分面。还有其他几个运算符可供使用
use \Aternos\ModrinthApi\Client\Options\Facets\FacetOperator; $facet = new Facet(FacetType::DOWNLOADS, "1.20.1", FacetOperator::GREATER_THAN);
获取额外的项目数据
项目包装器提供获取有关项目额外数据的方法。
// get a specific project $project = $modrinthClient->getProject("mclogs"); // get versions of the project $versions = $project->getVersions(); // get a specific version $version = $project->getVersion("2.6.2"); // get the members of the project $members = $project->getMembers();
获取项目
您也可以通过它们的 id 或 slug 获取单个项目
$project = $modrinthClient->getProject("mclogs");
或通过它们的 ids 获取多个项目
$projects = $modrinthClient->getProjects(["6DdCzpTL", "VPo0otUH"]);
看起来这里也支持通过 slug 获取,但这尚未记录。
检查是否使用了 slug/id
$projectId = $modrinthClient->checkProjectValidity("mclogs"); if ($projectId) { echo "project exists, id:" . $projectId; } else { echo "project does not exist"; }
项目依赖
获取项目的依赖项
$dependencies = $project->getDependencies(); // on modrinth you can depend on a project or directly on a version $projects = $dependencies->getProjects(); $versions = $dependencies->getVersions();
版本
// get versions of a project by name $versions = $modrinthClient->getProjectVersions("mclogs"); // get the versions from a project $versions = $project->getVersions(); // get a specific version by its id $version = $modrinthClient->getVersion("xzRGr4AC"); // get multiple versions by their ids $versions = $modrinthClient->getVersions(["xzRGr4AC", "EwNN8uNA"]);
散列
modrinth API 还允许您通过其 SHA1 或 SHA512 散列找到版本
use \Aternos\ModrinthApi\Client\HashAlgorithm; $hash = "5952253d61e199e82eb852c5824c3981b29b209d"; // returns the modrinth version for the given hash $version = $modrinthClient->getVersionByHash($hash, HashAlgorithm::SHA1); // fetch multiple versions at once $versions = $modrinthClient->getVersionsByHashes([$hash], HashAlgorithm::SHA1); // returns the latest version for a specific loader and version $version = $modrinthClient->getLatestVersionByHash($hash, ["spigot"], ["1.19.4"], HashAlgorithm::SHA1); // fetch multiple versions at once $versions = $modrinthClient->getLatestVersionsByHashes([$hash], ["spigot"], ["1.19.4"], HashAlgorithm::SHA1);
用户
// get a user $user = $modrinthClient->getUser("matthias"); // get projects of a user $projects = $user->getProjects(); // get notifications (requires authentication) $notifications = $user->getNotifications(); // get followed projects (requires authentication) $projects = $user->getFollowedProjects(); // get payout history (requires authentication) $history = $user->getPayoutHistory();
团队
// get the members of a team $members = $modrinthClient->getTeamMembers("ThaUQrOs"); // get the members of multiple teams at once $teams = $modrinthClient->getTeams(["ThaUQrOs"]);
标签
您可以从 API 获取所有可用的类别、加载器、游戏版本和许可证。我们的库提供直接通过这些方法搜索项目的方法
$categories = $modrinthClient->getCategories(); $categories[0]->searchProjects(); $loaders = $modrinthClient->getLoaders(); $loaders[0]->searchProjects(); $gameVersions = $modrinthClient->getGameVersions(); $gameVersions[0]->searchProjects(); $licenses = $modrinthClient->getLicenses(); $licenses[0]->searchProjects();
您还可以获取捐赠平台和报告类型
$reportTypes = $modrinthClient->getReportTypes(); $donationPlatforms = $modrinthClient->getDonationPlatforms();
更新生成的代码
可以通过安装 openapi 生成器 并运行以下命令来更新生成的代码
openapi-generator-cli generate -c config.yaml