Skip to content
Advertisement

Warning: Array to string conversion base64_encode

When I do this $payload2 = base64_encode($payload); I will get message “Expected type : string” so I change to $payload2 = base64_encode((string)$payload); but I will get the “Warning: Array to string conversion” error. How to solve this error?

$header = array(
            "alg" => "HS256",
            "typ" => "JWT", 
            );
$header2 =  base64_encode((string)$header);

Solution of using json_encode:

$header =json_encode(['typ' => 'JWT', 'alg' => 'HS256']);
$base64UrlHeader = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($header));

Advertisement

Answer

Solved by using implode()

$header = array(
            "alg" => "HS256",
            "typ" => "JWT", 
            );
$header2 = implode("",$header);
$header3 =  base64_encode($header2);
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement