· 5 years ago · Apr 14, 2020, 11:00 AM
1/*
2Contoh Laporan Kuliah Online
3NIM : 11170391
4Nama : Ilham Royani
5*/
6
7include("Api.php");
8
9/*$limit = isset($_GET['limit'])?(int)$_GET['limit']:0;
10$keyword = isset($_GET['name'])?(int)$_GET['name']:'';
11
12$products = new Api();
13echo $products->get($keyword,$limit);
14
15
16//*/
17
18require "vendor/autoload.php";
19use \Firebase\JWT\JWT;
20
21header("Access-Control-Allow-Origin: *");
22header("Content-Type: application/json; charset=UTF-8");
23header("Access-Control-Allow-Methods: POST");
24header("Access-Control-Max-Age: 3600");
25header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
26
27
28$secret_key = "ILhamRoy11170391";
29$jwt = null;
30$data = json_decode(file_get_contents("php://input"));
31
32
33$authHeader = $_SERVER['HTTP_AUTHORIZATION'];
34
35$arr = explode(" ", $authHeader);
36
37
38/*echo json_encode(array(
39 "message" => "sd" .$arr[1]
40));*/
41
42$jwt = $arr[1];
43
44if($jwt){
45
46 try {
47
48 $decoded = JWT::decode($jwt, $secret_key, array('HS256'));
49
50 // Access is granted. Add code of the operation here
51
52 $limit = isset($_GET['limit'])?(int)$_GET['limit']:0;
53 $keyword = isset($_GET['name'])?(int)$_GET['name']:'';
54
55 $products = new Api();
56 $data=$products->get($keyword,$limit);
57
58 echo $data;
59
60/* echo json_encode(array(
61 "message" => "Access granted:",
62 "data" => $data,
63 // "error" => $e->getMessage()
64 )); //*/
65
66 }catch (Exception $e){
67
68 http_response_code(401);
69
70 echo json_encode(array(
71 "message" => "Access denied.",
72 "error" => $e->getMessage()
73 ));
74}
75
76}