我正在构建一个API,但是在使用JWT创建我的令牌时出现Uncaught错误,当我运行一个post man调用时,我进入我的错误日志堆栈跟踪:
#0 [internal function]: Api->generateToken()
#1 /home/osconliz/public_html/Osconlizapicall/rest.php(42): ReflectionMethod->invoke(Object(Api))
#2 /home/osconliz/public_html/Osconlizapicall/index.php(4): Rest->processApi()
#3 {main}
thrown in /home/osconliz/public_html/Osconlizapicall/api.php on line 36
[19-May-2018 02:04:47 UTC] PHP Fatal error: Uncaught Error: Class 'JWT' not found in /home/osconliz/public_html/Osconlizapicall/api.php:36
Stack trace:
#0 [internal function]: Api->generateToken()
#1 /home/osconliz/public_html/Osconlizapicall/rest.php(42): ReflectionMethod->invoke(Object(Api))
#2 /home/osconliz/public_html/Osconlizapicall/index.php(4): Rest->processApi()
#3 {main}
但是当我在我的PHP服务器上检查我的jwt文件时,它上面有JWT类。
带有jwt类屏幕截图的 **jwt.php**
页面
然后我用来创建**api.php**
// SECRETE_KEY是为JWT创建传递的常量
<?php
class Api extends Rest {
public $dbConn;
public function __construct(){
parent::__construct();
$db = new DbConnect;
$this->dbConn = $db->connect();
}
public function generateToken(){
$client_id_key = $this->validateParameter('client_id_key', $this->param['client_id_key'], STRING);
//$client_secret_key = $this->validateParameter('client_secret_key', $this->param['client_secret_key'], STRING);
//client_secret_key should be commented out it is not used for validation for security purposes, only id key
$stmt = $this->dbConn->prepare("SELECT * FROM `api_clients_properties` WHERE client_id = :client_id_key");
$stmt->bindParam(":client_id_key", $client_id_key);
$stmt->execute();
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if (!is_array($user)){
$this->returnResponse(API_NAME_REQUIRED, "Invalid Client Id Key");
}
if ($user['property_status'] == "not verified"){
$this->returnResponse(API_NAME_REQUIRED, "Property not verified, please contact admin, to verify it");
}
$payload = [
'iat' => time(),
'iss' => 'localhost',
'exp' => time() + (60),
'userId' => $user['id']
];
$token = JWT::encode($payload, SECRETE_KEY);
echo $token;
}
}
?>
答案 0 :(得分:2)
JWT
类位于命名空间Firebase\JWT
中,因此您需要use
它:
use \Firebase\Jwt\Jwt;
Jwt::encode(...);
或者在调用时使用其完整命名空间:
\Firebase\Jwt\Jwt::encode();
答案 1 :(得分:1)
如果您从GitHub复制文件而不是使用composer来安装它,则需要注释掉文件顶部的命名空间行。所以,从jwt.php
屏幕截图第一行最顶部的快照中,您将注释掉//namespace Firebase\JWT;
,并且您不会再次收到500内部服务器错误。