Skip to content

Instantly share code, notes, and snippets.

@osor1o
Last active June 18, 2019 17:55
Show Gist options
  • Select an option

  • Save osor1o/a7a49da5fac9520808cff969976cc5c1 to your computer and use it in GitHub Desktop.

Select an option

Save osor1o/a7a49da5fac9520808cff969976cc5c1 to your computer and use it in GitHub Desktop.
Create a JWT (Json Web Token) with PHP
<?php
function encodeBase64Url(string $data) {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
$secret = "secret";
$header = encodeBase64Url(json_encode([
"alg" => "HS256",
"typ" => "JWT"
]));
$payload = encodeBase64Url(json_encode([
"id" => 122,
"username" => "test"
]));
$signature = encodeBase64Url(hash_hmac('sha256', "{$header}.{$payload}", $secret, true));
$jwt = "{$header}.{$payload}.{$signature}";
print $jwt . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment