greenhollowtech / ght-mojang-api-client
GHT Mojang API 客户端
1.1.0
2016-10-10 21:22 UTC
Requires
- php: >=5.5
- greenhollowtech/ght-api-client: ~3.0
Requires (Dev)
- php-mock/php-mock-phpunit: ~1.1
- phpunit/phpunit: ~5.4
README
Mojang API 客户端为 PHP 应用程序提供了访问 Mojang API 的方法。对于需要认证的 API 方法,客户端还提供了通过 Mojang 认证 API 进行认证的功能。
安装
获取 Composer 包
使用 Composer 安装,运行 composer require greenhollowtech/ght-mojang-api-client
。
用法
创建客户端实例
use GHT\MojangApiClient\GHTMojangApiClient;
// Instantiate the client
$client = GHTMojangApiClient::createApiClient();
$client = GHTMojangApiClient::createAuthenticationClient();
$client = GHTMojangApiClient::createSessionClient();
$client = GHTMojangApiClient::createStatusClient();
// The client can be be instantiated the long way
$targetApi = GHTMojangApiClient::SUBDOMAIN_API;
$client = GHTMojangApiClient::create($targetApi);
// Other targets
$targetApi = GHTMojangApiClient::SUBDOMAIN_OAUTH;
$targetApi = GHTMojangApiClient::SUBDOMAIN_SESSION;
$targetApi = GHTMojangApiClient::SUBDOMAIN_STATUS;
然后使用客户端调用以下描述的相关方法。
API 方法
基本 API
$client = GHTMojangApiClient::createApiClient();
// Get all names a user has ever used
$names = $client->getNames($uuid);
// Get the UUID for the user currently or in the past using a given name
$profile = $client->getProfileForName('SomeDude');
$profile = $client->getProfileForName('SomeDude', new \DateTime('-1 month'));
// Get the UUIDs (and possibly other limited information) of a list of names
$profiles = $client->getProfilesForNames(array('SomeDude', 'SomeOtherDude'));
// Get Mojang statistics
$statistics = $client->getStatistics();
$metrics = array('item_sold_minecraft', 'item_sold_scrolls');
$statistics = $client->getStatistics($metrics);
$splitByMetric = true;
$statistics = $client->getStatistics($metrics, $splitByMetric);
认证 API
use GHT\MojangApiClient\Exception\MojangApiException;
$authClient = GHTMojangApiClient::createAuthenticationClient();
try {
// Client will transport the credentials to use with other API requests
$authClient->authenticate($email, $password);
// Authenticating with a game as the agent provides better credentials
$authClient->authenticate($email, $password, 'Minecraft');
// Refreshes the API token transported in the client
$authClient->refresh();
// Will throw an exception if not valid, otherwise returns nothing
$authClient->validate();
// End the authenticated session by invalidating the token
$authClient->invalidate();
// End the authenticated session using the login name and password
$authClient->signOut($email, $password);
}
catch (MojangApiException $e) {
echo $e->getMessage();
}
带认证的基本 API
use GHT\MojangApiClient\Exception\MojangApiException;
// Use an authentication client to get the credentials for the API client
$authClient = GHTMojangApiClient::createAuthenticationClient();
try {
$authClient->authenticate($email, $password);
}
catch (MojangApiException $e) {
// login failed, go somewhere else
}
$client = GHTMojangApiClient::createApiClient($authClient->getCredentials());
// Change a skin
try {
$client->changeSkin($uuid, $url, 'slim');
}
catch (MojangApiExceptin $e) {
echo $e->getMessage();
}
// Get the current user's information
$userInfo = $client->getUserInformation();
// Reset the user's skin
try {
$client->resetSkin($uuid);
}
catch (MojangApiExceptin $e) {
echo $e->getMessage();
}
// Upload a skin
try {
$client->uploadSkin($uuid, '/tmp/skinFile.png', 'slim');
}
catch (MojangApiExceptin $e) {
echo $e->getMessage();
}
会话 API
$client = GHTMojangApiClient::createSessionClient();
// Get blocked server hashes
$hashes = $client->getBlockedServerHashes();
$serverIsBlocked = in_array(sha1('*.myservername.com'), $hashes);
// Get profile data for a UUID
$profile = $client->getProfile($uuid);
状态 API
// Get statuses for all services
$statuses = $client->getStatus();
$minecraftStatus = $statuses['minecraft.net'];