JWT::jsonEncode PHP Method

jsonEncode() public static method

Encode a PHP object into a JSON string.
public static jsonEncode ( object | array $input ) : string
$input object | array A PHP object or array
return string JSON representation of the PHP object or array
    public static function jsonEncode($input)
    {
        $json = json_encode($input);
        if (function_exists('json_last_error') && ($errno = json_last_error())) {
            JWT::handleJsonError($errno);
        } elseif ($json === 'null' && $input !== null) {
            throw new DomainException('Null result with non-null input');
        }
        return $json;
    }

Usage Example

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, $algo = 'HS256')
 {
     $header = array('typ' => 'jwt', 'alg' => $algo);
     $segments = array();
     $segments[] = JWT::urlsafeB64Encode(JWT::jsonEncode($header));
     $segments[] = JWT::urlsafeB64Encode(JWT::jsonEncode($payload));
     $signing_input = implode('.', $segments);
     $signature = JWT::sign($signing_input, $key, $algo);
     $segments[] = JWT::urlsafeB64Encode($signature);
     return implode('.', $segments);
 }
All Usage Examples Of JWT::jsonEncode