This is a simple library for working with Json Web Token in PHP.
Installing
$ composer require archphp/jwt
How to use
<?phprequire_once'{path/to/vendor}/autoload.php';
useArch\JWT\JWT;
useArch\JWT\Token\Header;
useArch\JWT\Token\Payload;
useArch\JWT\Token\Algorithm\HS256;
useArch\JWT\Token\Claim\Subject;
$alg = newHS256('secret');
$typ = 'JWT';
$header = newHeader($alg, $typ);
$payload = newPayload([
Subject::$name => 'arch',
'custom_claim' => 'custom_value'
]);
/** * @see \Arch\JWT\Token\Claim for all available claims */$jwt = newJWT($header, $payload);
// This will print out the encrypted JWT.echo$jwt->getJWT();
Decoding a JWT
<?phpuseArch\JWT\JWT;
useArch\JWT\Exception\InvalidJWTException;
useArch\JWT\Exception\VerifierException;
$t = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJBcmNoIn0.kq-ax6J9QPjDcT9Gx4ZPwP-L-FBY_rnEE5i_2ec2X7o';
// The secret key used in encrypting the JWT.$secret = 'Arch01';
// JWT::decode will throw an Exception if the JWT is invalid or claims are not verified.try {
$jwt = JWT::decode($t, $secret);
} catch(InvalidJWTException|VerifierException$e) {
echo$e->getMessage();
}