PHP code example of phithi92 / json-web-token

1. Go to this page and download the library: Download phithi92/json-web-token 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/ */

    

phithi92 / json-web-token example snippets


use Phithi92\JsonWebToken\JsonWebToken;
use Phithi92\JsonWebToken\PayloadBuilder;
use Phithi92\JsonWebToken\Security\Openssl;

$payload = (new PayloadBuilder())
    ->setExpiration('+15min')
    ->setIssuer('issuer')
    ->setAudience('audience')
    ->addField('your-field', 'your-data');

$openssl = (new Openssl())
    ->setPrivateKey('your-private-key')
    ->setPublicKey('your-public-key');

$jwt = new JsonWebToken($openssl);
$jwtToken = $jwt->create($payload, '', 'JWE', 'RSA-OAEP+A128KW');

$payload = (new PayloadBuilder())
    ->setExpiration('+15min')
    ->setIssuer('issuer')
    ->setAudience('audience')
    ->addField('custom', 'dont.know');

$jwt = new JsonWebToken(new Openssl());
$jwtToken = $jwt->create($payload, $secret,'JWS', 'HS256');

try {
    $isValid = $jwt->validateToken($token, $key);
} catch (InvalidTokenException $e) {
    // Handle invalid token
} catch (UnsupportedAlgorithmException $e) {
    // Handle unsupported algorithm
}