目前 PHP 所有的 JWT 库都是用 PHP 语言编写的,为了提高性能,我基于 openssl 库,用 PHP 扩展实现了一个 JWT
GitHub: https://github.com/cdoco/php-jwt
例子
$key = "example-hmac-key";
$claims = array(
"data" => [
"name" => "ZiHang Gao",
"admin" => true
],
"iss" => "http://example.org",
"sub" => "1234567890",
);
// default HS256 algorithm
$token = jwt_encode($claims, $key);
echo $token . PHP_EOL;
//eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.
//eyJpc3MiOiJodHRwOlwvXC9leGFtcGxlLm9yZyIsInN1YiI6IjEyMzQ1Njc4OTAiLCJuYW1lIjoiWmlIYW5nIEdhbyIsImFkbWluIjp0cnVlfQ.
//2lFeBTsRegsjXiBCZNkW41KFlsZPSFu7KTsyAM9lUiQ
print_r(jwt_decode($token, $key));
/**
Array
(
[data] => Array
(
[name] => ZiHang Gao
[admin] => 1
)
[iss] => http://example.org
[sub] => 1234567890
)
*/