Example #1
0
 /**
  * @param object|array $payload PHP object or array
  * @param string       $key     The secret key
  * @param string       $algo    The signing algorithm
  *
  * @return string A JWT
  */
 public static function encode($payload, $key, $type, $algo = 'HS256')
 {
     $header = array('typ' => 'JWT', 'alg' => $algo);
     $segments = array();
     $tmp_str = implode(',', $payload);
     $segments[] = JWT::urlsafeB64Encode(JWT::jsonEncode($header));
     if ($type) {
         $segments[] = JWT::urlsafeB64Encode_parse(JWT::jsonEncode($payload));
     } else {
         $segments[] = JWT::urlsafeB64Encode(JWT::jsonEncode($payload));
     }
     $signing_input = implode('.', $segments);
     $signature = JWT::sign($signing_input, $key, $algo);
     $segments[] = JWT::urlsafeB64Encode($signature);
     return implode('.', $segments);
 }