PHP code example of gamegos / jwt

1. Go to this page and download the library: Download gamegos/jwt 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/ */

    

gamegos / jwt example snippets


$key = 'some-secret-for-hmac';
$alg = 'HS256';

$token = new \Gamegos\JWT\Token();
$token->setClaim('sub', '[email protected]'); // alternatively you can use $token->setSubject('[email protected]') method
$token->setClaim('exp', time() + 60*5);

$encoder = new \Gamegos\JWT\Encoder();
$encoder->encode($token, $key, $alg);

printf("JWT TOKEN: %s\n", $token->getJWT()); //eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJzb21lb25lQGV4YW1wbGUuY29tIiwiZXhwIjoxNDA4NDUzNzkwfQ.2Fk5-UUMhOAcQH812LL0sdaf29zuf293nLhHp_VKBDg




$jwtString = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJzb21lb25lQGV4YW1wbGUuY29tIiwiZXhwIjoxNDA4NDUyMzcxfQ.Fy1DLdfZBiR_khyTsghItDW3_1rM7osz_IxjiaiRto0';

$key = 'some-secret-for-hmac';


try {
    $validator = new \Gamegos\JWT\Validator();
    $token = $validator->validate($jwtString, $key);

    print_r($token->getClaims());
    print_r($token->getHeaders());
} catch (\Gamegos\JWT\Exception\JWTException $e) {
    printf("Invalid Token:\n  %s\n", $e->getMessage());
    //$e->getToken();
}