搜尋

首頁  >  問答  >  主體

重寫後的標題為:keyOrKeyArray必須是Firebase\\JWT\\Key鍵的實例或Firebase\\JWT\\Key鍵的數組,在php ci4中

{ "title": "UnexpectedValueException", "type": "UnexpectedValueException", 「代碼」:500, "message": "$keyOrKeyArray 必須是 FirebaseJWTKey 鍵的實例或 FirebaseJWTKey 鍵的陣列", "檔案": "E:varwwwhtmlprojectspansadminvendorfirebasephp-jwtsrcJWT.php", 「行」:416,

P粉680000555P粉680000555386 天前580

全部回覆(1)我來回復

  • P粉914731066

    P粉9147310662023-11-08 11:59:41

    您可以使用下面的範例程式碼 -

    <?php
    
    namespace App\Controllers;
    
    use App\Controllers\BaseController;
    use Firebase\JWT\JWT;
    use Firebase\JWT\Key;
    
    class User extends BaseController
    {
        public function exampleMethod()
        {
            $issuedAt = time();
            $expirationTime = $issuedAt + 60;  // jwt valid for 60 seconds from the issued time
            $payload = array( // Any random data
                'userid' => 'Test_UID',
                'name' => 'Sankhnad Mishra',
                'iat' => $issuedAt,
                'exp' => $expirationTime
            );
            $key = 'A_JWT_SECRET'; // Any string
            $alg = 'HS256'; // This is alg
    
            $token = JWT::encode($payload, $key, $alg); // Encode payload as a JWT Token
            $decodedToken = JWT::decode($token, new Key($key, 'HS256')); // Decode token to a payload
    
            $response = [
                'token' => $token,
                'decodedToken' => $decodedToken
            ];
    
            print_r($response);
        }
    }

    如果您在上面運行,那麼您將得到以下回應 -

    {
        "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyaWQiOiJUZXN0X1VJRCIsIm5hbWUiOiJTYW5raG5hZCBNaXNocmEiLCJpYXQiOjE2NDU4MTU0OTUsImV4cCI6MTY0NTgxNTU1NX0.0CwT9quW8-teyob3ObRU5KQBfQYWamCSTVCrAk9UX-o",
        "decodedToken": {
            "userid": "Test_UID",
            "name": "Sankhnad Mishra",
            "iat": 1645815495,
            "exp": 1645815555
        }
    }

    因此,只需根據您的要求從上面選擇這些程式碼即可。

    回覆
    0
  • 取消回覆