JWT::encode( object|array $payload, string|resource $key, string $alg = 'HS256', mixed $keyId = null, array $head = null )

Converts and signs a PHP object or array into a JWT string.


Parameters Parameters

$payload

(object|array) (Required) PHP object or array

$key

(string|resource) (Required) The secret key. If the algorithm used is asymmetric, this is the private key

$alg

(string) (Optional) The signing algorithm. Supported algorithms are 'ES384','ES256', 'HS256', 'HS384', 'HS512', 'RS256', 'RS384', and 'RS512'

Default value: 'HS256'

$keyId

(mixed) (Optional)

Default value: null

$head

(array) (Optional) An array with header elements to attach

Default value: null


Top ↑

Return Return

(string) A signed JWT


Source Source

File: vendor/firebase/php-jwt/src/JWT.php

    public static function encode($payload, $key, $alg = 'HS256', $keyId = null, $head = null)
    {
        $header = array('typ' => 'JWT', 'alg' => $alg);
        if ($keyId !== null) {
            $header['kid'] = $keyId;
        }
        if (isset($head) && \is_array($head)) {
            $header = \array_merge($head, $header);
        }
        $segments = array();
        $segments[] = static::urlsafeB64Encode(static::jsonEncode($header));
        $segments[] = static::urlsafeB64Encode(static::jsonEncode($payload));
        $signing_input = \implode('.', $segments);

        $signature = static::sign($signing_input, $key, $alg);
        $segments[] = static::urlsafeB64Encode($signature);

        return \implode('.', $segments);
    }