publicuhc/php-yggdrasil

与Mojang认证服务器交互的PHP库

1.0.2 2016-06-22 15:16 UTC

This package is not auto-updated.

Last update: 2024-09-25 12:46:14 UTC


README

SensioLabsInsight

与Mojang认证服务器交互的PHP库。

安装

使用composer将其添加到要求部分

{
    "require-dev": {
        "publicuhc/php-yggdrasil": "dev-master"
    }
}

用法

首先您需要获取一个 Yggdrasil 实例,目前唯一的选择是 DefaultYggdrasil

$yggdrasil = new DefaultYddrasil();

如果需要,您可以在构造函数中传递用户名/clientToken/accessToken,或者使用设置方法。

然后您可以使用Yggdrasil实例向服务器查询

//set the username
$yggdrasil->setUsername('joe@blogs.com');

//authenticate with the password, sets accessToken/clientToken on success
$yggdrasil->authenticate('joeisthebest');

$clientToken = $yggdrasil->getClientToken();
$accessToken = $yggdrasil->getAccessToken();

错误检查

所有API函数都可以抛出以下异常

APIRequestException

当Mojang服务器返回请求错误时抛出,简短错误信息可以在 $ex->getShortMessage() 中找到,完整信息可以在 $ex->getMessage() 中找到,原因(如果已设置)可以在 $ex->getCause() 中找到

InvalidParameterException

当调用API方法且某些参数尚未设置时抛出。例如。

//username not set yet
$yg = new DefaultYggdrasil();

//will throw InvalidParameterException due to username not being set
$yg->authenticate('xxx');

$yg->setUsername('yyy');

//will run correctly
$yg->authenticate('xxx');