【PHP】【Too few arguments to function Firebase\JWT\JWT::encode()。。。。。。。】

1.安装jwt

composer require firebase/php-jwt
use Firebase\JWT\JWT;

   public function hello($name = 'ThinkPHP5')
    {

        $secret_key = "YOUR_SECRET_KEY";
        $issuer_claim = "THE_ISSUER";
        $audience_claim = "THE_AUDIENCE";
        $issuedat_claim = time(); // issued at
        $notbefore_claim = $issuedat_claim + 10; //not before in seconds
        $expire_claim = $issuedat_claim + 60; // expire time in seconds
        $token = array(
            "iss" => $issuer_claim,
            "aud" => $audience_claim,
            "iat" => $issuedat_claim,
            "nbf" => $notbefore_claim,
            "exp" => $expire_claim,
            "data" => array(
                "user_id" => "1",
                "user_name" => "John Doe",
            )
        );

        $jwt = JWT::encode($token, $secret_key);

        echo $jwt;
        // return 'hello,' . $name;
    }

上面的

$jwt = JWT::encode($token, $secret_key);

改为

 $jwt = JWT::encode($token, $secret_key, "HS256");

2.jwt解析(“firebase/php-jwt”: “^6.4” 的版本)

use Firebase\JWT\JWT;
use Firebase\JWT\Key;

$jwt = JWT::encode($payload, $key, 'HS256');
//解析
$decoded = JWT::decode($jwt, new Key($key, 'HS256'));

你可能感兴趣的:(PHP,php,开发语言)