Skip to main content

How to Base64url-Encode a String in PHP with JWT::urlsafeB64Encode

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

PHP's own base64_encode writes standard base64, with +, / and = padding. JSON Web Tokens use base64url instead, and php-jwt, the PHP JWT library, has a public helper for it: JWT::urlsafeB64Encode.

Syntax​

Syntax
use Firebase\JWT\JWT;

$<encoded> = JWT::urlsafeB64Encode(<text>);
PartWhat it does
JWT::urlsafeB64Encode(<text>)Encodes the string as base64, then swaps + and / for - and _ and removes the = padding.
<text>Any string, including binary data.

php-jwt's source documents urlsafeB64Encode as: "Encode a string with URL-safe Base64." Its code is one line: str_replace('=', '', strtr(base64_encode($input), '+/', '-_')).

The Example​

The example encodes the text Hi?> with base64_encode and with JWT::urlsafeB64Encode. Those four characters were chosen because their base64 contains a / and padding.

Prerequisites​

  • PHP and Composer. This guide uses PHP 8.6.0 and Composer 2.11.3.

Step 1: Create the Project​

Terminal
mkdir demo
cd demo
composer require firebase/php-jwt

composer require downloads php-jwt, version 7.2.2 here, into a vendor folder and writes vendor/autoload.php, which the code loads.

Step 2: Write the Code​

Create main.php in the demo folder:

main.php, which loads vendor/autoload.php, imports Firebase\JWT\JWT, and echoes base64_encode of Hi?> and JWT::urlsafeB64Encode of Hi?>.

main.php
<?php

require __DIR__ . '/vendor/autoload.php';

use Firebase\JWT\JWT;

echo base64_encode('Hi?>'), "\n";
echo JWT::urlsafeB64Encode('Hi?>'), "\n";

Step 3: Run It​

Terminal
php main.php
Output
SGk/Pg==
SGk_Pg

A zsh terminal where php main.php prints SGk/Pg== and SGk_Pg.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • base64_encode gave SGk/Pg==: a / and two = of padding.
  • JWT::urlsafeB64Encode gave SGk_Pg: the / became _, and the padding is gone, which is the form every piece of a JWT uses.

Common Mistakes​

MistakeWhat happensDo this instead
Using base64_encode for a JWT pieceThe text can contain +, / and =Use JWT::urlsafeB64Encode
Forgetting require __DIR__ . '/vendor/autoload.php'Error: Class "Firebase\JWT\JWT" not foundLoad the autoloader first
Encoding the result twiceYou get base64 of base64Encode the original string once

Troubleshooting​

SymptomLikely causeFix
Class "Firebase\JWT\JWT" not foundThe autoloader isn't loaded, or php-jwt isn't installed in this folderRun composer require firebase/php-jwt and require the autoloader
The result ends in =It came from base64_encodeUse JWT::urlsafeB64Encode

FAQ​

Can I do this without php-jwt?​

Yes, with the same one line php-jwt uses: str_replace('=', '', strtr(base64_encode($text), '+/', '-_')).

How do I decode it again?​

With JWT::urlsafeB64Decode, which adds the padding back and swaps the characters before decoding.

Conclusion​

JWT::urlsafeB64Encode($text) turns a string into the unpadded base64url that JWTs use. It comes with php-jwt, so code that already uses the library needs nothing more.

Additional Resources​