Reputation: 433
I am building an API, but I get an uncaught error when creating my token with JWT, when I run a postman call, I get in my error log Stack trace:
#0 [internal function]: Api->generateToken()
#1 /home/example/public_html/Exampleapicall/rest.php(42): ReflectionMethod->invoke(Object(Api))
#2 /home/example/public_html/Exampleapicall/index.php(4): Rest->processApi()
#3 {main}
thrown in /home/example/public_html/Exampleapicall/api.php on line 36
[19-May-2018 02:04:47 UTC] PHP Fatal error: Uncaught Error: Class 'JWT' not found in /home/example/public_html/Exampleapicall/api.php:36
Stack trace:
#0 [internal function]: Api->generateToken()
#1 /home/example/public_html/Exampleapicall/rest.php(42): ReflectionMethod->invoke(Object(Api))
#2 /home/example/public_html/Exampleapicall/index.php(4): Rest->processApi()
#3 {main}
but when I check my jwt file on my PHP server it has class JWT in it.
**jwt.php**
page with class jwt screenshot
Then the page I am using to create the token which is **api.php**
.
//SECRETE_KEY is a constant for creating a pass for 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;
}
}
?>
Upvotes: 0
Views: 2779
Reputation: 433
If you copied the file from GitHub rather than using composer to install it, you will need to comment out the namespace lines at the top of the file. so from my snapshot at the very top of the first line of jwt.php
screenshot, you will comment out //namespace Firebase\JWT;
and you would not get the 500 internal server error again.
Upvotes: -1
Reputation: 53581
The JWT
class is in the namespace Firebase\JWT
, so you will either need to use
it:
use \Firebase\Jwt\Jwt;
Jwt::encode(...);
Or use its full namespace when invoking:
\Firebase\Jwt\Jwt::encode();
Upvotes: 2