JWT(JSON Web Token) (Third Token) Creation with CURL in CodeIgniter 3
<?php
defined("BASEPATH") or exit("No direct script access allowed");
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
class RequestThree extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$key = "other_key";
$payload = [
"id" => 12,
"name" => "Patel Ramesh Guatambhai",
"email" => "patelramesh8762@gmail.com",
"address" => "Nikol, Ahmedabad",
"Roll No" => "44",
"School Name" => "Navrang School",
"Time" => "12 PM to 5 PM",
"Position" => "Software Developer",
"city" => "Ahmedabad",
"pincode" => "342212",
"iat" => time()
];
$jwt = JWT::encode($payload, $key, "HS256");
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'http://localhost/blogApi/ApiThree/users',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => [
'Accept: application/json',
'Authorization: Bearer ' . $jwt,
],
]);
$response = curl_exec($curl);
curl_close($curl);
print_r($response);
}
}
ApiThree Decode data File
<?php
defined("BASEPATH") or exit("No direct script access allowed");
use chriskacerguis\RestServer\RestController;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
class ApiThree extends RestController
{
public function __construct()
{
parent::__construct();
}
public function users_get()
{
$key = "other_key";
$headers = apache_request_headers();
$jwt = $headers["Authorization"];
$jwt = str_replace("Bearer", "", $jwt);
$jwt = trim($jwt);
print_r($jwt);
echo "<br><br><br>";
try {
$decoded = JWT::decode($jwt, new Key("other_key", "HS256"));
echo json_encode(array(
"status" => "success",
"data" => $decoded
));
echo "<br><br><br>";
echo "<pre>";
$this->response($decoded);
} catch (\Firebase\JWT\ExpiredException $e) {
echo json_encode(array(
"status" => "error",
"message" => "Token is Expired"
));
} catch (Exception $th) {
echo json_encode(array(
"status" => "error",
"message" => $th->getMessage()
));
print_r($th->getMessage());
exit();
}
}
}

Comments
Post a Comment