JWT(JSON Web 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 Request extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $key = "example_key";
        $payload = [
            "id" => 24,
            "name" => "Heer Nayakpara",
            "email" => "heernayakpara997@gmail.com",
            "address" => "Odhav, Ahmedabad",
            "iat" => time()
        ];

        $jwt = JWT::encode($payload, $key, 'HS256');

        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => 'http://localhost/blogApi/Api/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);
    }
}








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 Api extends RestController
{
    public function __construct()
    {
        parent::__construct();
    }
    public function users_get()
    {
        $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('example_key', 'HS256'));
            echo json_encode(array(
                "status" => "success",
                "data" => $decoded
            ));
            echo "<br><br><br>";
            $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();
        }

        // Users from a data store e.g. database
        /*$users = [
            ['id' => 0, 'name' => 'John', 'email' => 'john@example.com'],
            ['id' => 1, 'name' => 'Jim', 'email' => 'jim@example.com'],
        ];

        $id = $this->get('id');

        if ($id === null) {
            // Check if the users data store contains users
            if ($users) {
                // Set the response and exit
                $this->response($users, 200);
            } else {
                // Set the response and exit
                $this->response([
                    'status' => false,
                    'message' => 'No users were found'
                ], 404);
            }
        } else {
            if (array_key_exists($id, $users)) {
                $this->response($users[$id], 200);
            } else {
                $this->response([
                    'status' => false,
                    'message' => 'No such user found'
                ], 404);
            }
        }*/
    }
}


Comments