PHP code example of archphp / jwt

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

    

archphp / jwt example snippets




rch\JWT\JWT;
use Arch\JWT\Token\Header;
use Arch\JWT\Token\Payload;
use Arch\JWT\Token\Algorithm\HS256;
use Arch\JWT\Token\Claim\Subject;

$alg = new HS256('secret');
$typ = 'JWT';

$header = new Header($alg, $typ);

$payload = new Payload([
  Subject::$name => 'arch',
  'custom_claim' => 'custom_value'
]);

/**
 * @see \Arch\JWT\Token\Claim for all available claims
 */

$jwt = new JWT($header, $payload);

// This will print out the encrypted JWT.
echo $jwt->getJWT();



use Arch\JWT\JWT;
use Arch\JWT\Exception\InvalidJWTException;
use Arch\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();
}