<?php
class Base64Url
{
/**
* @param $str string
*
* @return string
*/
public static function encode($str)
{
$std = @base64_encode($str);
if ($std === false) {
throw new \Exception('base64_encode() failed');
}
return rtrim(strtr($std, '+/', '-_'), '=');
}
/**
* @param $str string
*
* @return string
*/
public static function decode($str)
{
$result = '';
if ($str !== null) {
if (!is_string($str)) {
throw new \Exception('Only strings can be decoded');
}
$std = strtr($data, '-_', '+/').str_repeat('=', strlen($data) % 4);
$result = base64_decode($std, true);
if ($result === false) {
throw new \Exception('base64_decode() failed');
}
}
return $result;
}
}
$ph = '{"typ":"JWT","alg":"HS256"}';
echo Base64Url::encode($ph);
- Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9
preferences:
164.92 ms | 404 KiB | 198 Q