/** * Converts and signs a PHP object or array into a JWT string. * * @param object|array $payload PHP object or array * @param string $key The secret key * @param string $algo The signing algorithm. Supported * algorithms are 'HS256', 'HS384' and 'HS512' * * @return string A signed JWT * @uses jsonEncode * @uses urlsafeB64Encode */ public static function encode($payload, $key, $algo = 'HS256') { $header = array('typ' => 'JWT', 'alg' => $algo); $header_en = JWT::base64url_encode(JWT::jsonEncode($header)); $payload_en = JWT::base64url_encode(JWT::jsonEncode($payload)); $segments = array(); $segments[] = JWT::base64url_encode(JWT::jsonEncode($header)); $segments[] = JWT::base64url_encode(JWT::jsonEncode($payload)); $signing_input = implode('.', $segments); $signature = JWT::sign($signing_input, $key, $algo); $segments[] = JWT::base64url_encode($signature); return implode('.', $segments); }