1. Go to this page and download the library: Download digitickets/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/ */
digitickets / jwt example snippets
use DigiTickets\Jwt\Config\JwtConfigInterface;
use Firebase\JWT\Key;
class MyJwtConfig implements JwtConfigInterface
{
public function getKey(): Key
{
return new Key($_ENV['MY_JWT_SECRET'], 'HS256');
}
public function getDefaultExpiresInMinutes(): int
{
return 1;
}
}
use DigiTickets\Jwt\JwtClaims;
use DigiTickets\Jwt\JwtData;
use DigiTickets\Jwt\JwtFactory;
$token = JwtFactory::createJwt(
new MyJwtConfig(), // The config created in step 1.
new JwtData([
JwtClaims::AUDIENCE => 'my-service',
])
);
var_dump($token); // string(153) "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3NzM0MzU5NjEsImV4cCI6MTc3MzQzNjAyMSwiYXVkIjoibXktc2VydmljZSJ9.7Y8Sr7RL9w453TuSpC-j5u8ZicbN6QSUtInu6xc2VpA"
// Expires in 30 days
$token = JwtFactory::createJwt($config, $jwtData, 1440 * 30);
// Never expires
$token = JwtFactory::createJwt($config, $jwtData, 0);