PHP code example of aternos / hangar-api

1. Go to this page and download the library: Download aternos/hangar-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 / hangar-api example snippets



use Aternos\HangarApi\Client\HangarAPIClient;

// create an API client. This is the main entry point for the API
$hangarClient = new HangarAPIClient();

// set a user agent (recommended)
$hangarClient->setUserAgent('aternos/php-hangar-api-example');

// set an api key (optional)
$hangarClient->setApiKey("api-key");

$projects = $hangarClient->getProjects();

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()->getName() . PHP_EOL;
}

$projects = $projects->getNextPage();

foreach ($projects as $project) {
    echo $project->getData()->getName() . PHP_EOL;
}

use \Aternos\HangarApi\Client\Options\ProjectSearch\ProjectSearchOptions;
use \Aternos\HangarApi\Client\Options\ProjectCategory;
use \Aternos\HangarApi\Client\Options\ProjectSearch\ProjectSortField;

$options = new ProjectSearchOptions();
$options->setCategory(ProjectCategory::ADMIN_TOOLS);
$options->setQuery("mclogs");
$options->setSortField(ProjectSortField::UPDATED);
$projects = $hangarClient->getProjects($options);

// get a specific project
$project = $hangarClient->getProject("mclogs");

// get versions of the project (paginated)
$versions = $project->getVersions();

// get a specific version
$version = $project->getVersion("2.6.2");

// get the owner of the project
$owner = $project->getOwner();

// get the members of the project (paginated)
$members = $project->getMembers();

// get the people who starred the project (paginated)
$stargazers = $project->getStargazers();

// get the people who are watching the project (paginated)
$watchers = $project->getWatchers();

// get versions of a project by name (paginated)
$versions = $hangarClient->getProjectVersions("mclogs");

// get the versions from a project (paginated)
$versions = $project->getVersions();

// get a specific version of a project by name
$version = $hangarClient->getProjectVersion("mclogs", "2.6.2");

// get a specific version of a project
$version = $project->getVersion("2.6.2");

// get the daily stats of the version
$stats = $version->getDailyStats();
foreach ($stats as $date => $stat) {
    echo $stat->getData()->getDownloads() . " Downloads and on " $date . PHP_EOL;
}

// get a user
$user = $hangarClient->getUser("Aternos");

// get all projects of a user (paginated)
$projects = $user->getProjects();

// get the projects a user has starred (paginated)
$starredProjects = $user->getStarredProjects();

// get the projects a user is watching (paginated)
$watchedProjects = $user->getWatchedProjects();

// get the main page of a project
$page = $hangarClient->getProjectMainPage("mclogs");

// get other pages
$page = $hangarClient->getProjectPage("mclogs", "Config");

// get a page from a project
$page = $project->getPage("Config");

// edit a page
$page->setContent("New content");
$page->save();