From f9eb6c4095934e4d0b8cb679ae626e59b54634db Mon Sep 17 00:00:00 2001 From: Rick Kuzik Date: Fri, 2 May 2025 12:22:09 -0600 Subject: [PATCH] REST API setup --- .gitignore | 2 ++ .htaccess | 12 +++++++++ api.php | 55 ++++++++++++++++++++++++++++++++++++++++ test.php | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+) create mode 100644 .gitignore create mode 100644 api.php create mode 100644 test.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..50a0c39 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*~ +node_modules/ diff --git a/.htaccess b/.htaccess index 7dd428b..3df6f34 100644 --- a/.htaccess +++ b/.htaccess @@ -4,7 +4,19 @@ Require ip 96.53.11.174 Require ip 127.0.0.1 Require ip 137.184.238.236 +Require ip 209.91.92.98 #Require all granted +RewriteEngine On +# IMPORTANT: RewriteBase must match the folder path +RewriteBase /admin/ +# Case: /admin/ → /admin/index.php +RewriteRule ^$ index.php [L] + +# Case: /admin/something → /admin/something.php/ +# Case: /admin/something/extra → /admin/something.php/extra +RewriteCond %{REQUEST_FILENAME} !-f +RewriteCond %{REQUEST_FILENAME} !-d +RewriteRule ^([^/]+)(/.*)?$ $1.php$2 [L,QSA] diff --git a/api.php b/api.php new file mode 100644 index 0000000..e859db5 --- /dev/null +++ b/api.php @@ -0,0 +1,55 @@ +endpoint = $endpoint; + $this->apiKey = $apiKey; + } + function _call() { + $ch = curl_init($endpoint); + + curl_setopt_array($ch, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_HTTPHEADER => [ + 'Accept: application/json', + 'Content-Type: application/json', + "X-API-Key: $apikey", + ], + ]); + + $response = curl_exec($ch); + + if (curl_errno($ch)) { + curl_close($ch); + throw new Exception(curl_error($ch)); + } else { + curl_close($ch); + print_r($httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + return (object)[ 'status' => httpCode, 'response' => json_decode($response)]; + } + function add($payload) { + $ch = curl_init($endpoint); + + curl_setopt_array($ch, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POST => true, + CURLOPT_HTTPHEADER => [ + 'Accept: application/json', + 'Content-Type: application/json', + "X-API-Key: $apikey", + ], + CURLOPT_POSTFIELDS => json_encode($payload), + ]); + + $response = curl_exec($ch); + + if (curl_errno($ch)) { + curl_close($ch); + throw new Exception(curl_error($ch)); + } else { + curl_close($ch); + print_r($httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + return (object)[ 'status' => httpCode, 'response' => json_decode($response)]; + } + } + +} diff --git a/test.php b/test.php new file mode 100644 index 0000000..c315a7c --- /dev/null +++ b/test.php @@ -0,0 +1,74 @@ + req_isjson(), + 'method' => req_method(['GET','POST']), + 'path' => $_SERVER['PATH_INFO'] ?? '/', + 'post' => $post, +]; + +$res = (object)[ + 'result' => 'ok', + 'req' => $req, +]; + + +header('Content-Type: application/json'); +echo json_encode([$res], JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT); +