PHP code example of aternos / modrinth-api

1. Go to this page and download the library: Download aternos/modrinth-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aternos / modrinth-api example snippets



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");

$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);

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();

$project = $modrinthClient->getProject("mclogs");

$projects = $modrinthClient->getProjects(["6DdCzpTL", "VPo0otUH"]);

$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"]);

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 (et payout history (

// get the members of a team
$members = $modrinthClient->getTeamMembers("ThaUQrOs");

// get the members of multiple teams at once
$teams = $modrinthClient->getTeams(["ThaUQrOs"]);

$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();