REST API setup

api
Rick Kuzik 9 months ago
parent 3a11453dd1
commit f9eb6c4095
  1. 2
      .gitignore
  2. 12
      .htaccess
  3. 55
      api.php
  4. 74
      test.php

2
.gitignore vendored

@ -0,0 +1,2 @@
*~
node_modules/

@ -4,7 +4,19 @@ Require ip 96.53.11.174
Require ip 127.0.0.1 Require ip 127.0.0.1
Require ip 137.184.238.236 Require ip 137.184.238.236
Require ip 209.91.92.98
#Require all granted #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]

@ -0,0 +1,55 @@
<?php
class Redirect {
__construct($endpoint, $apiKey) {
$this->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)];
}
}
}

@ -0,0 +1,74 @@
<?php
/*
* New PHP REST Framework
*
* Using Following SPECS
* - HTTP Status Codes https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status
* - HTTP Methods https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Methods
* - REST API https://restfulapi.net/http-methods/
*/
/**
* Determines whether the incoming request has a JSON-based Content-Type.
*
* @param string|null $contentType Optional override for testing or CLI use.
* @return bool True if the Content-Type indicates JSON.
*/
function req_isjson(?string $contentType = null): bool {
// Get the header from the server if not provided
$type = $contentType ?? ($_SERVER['CONTENT_TYPE'] ?? '');
// Normalize and strip parameters like "; charset=utf-8"
$type = strtolower(trim(explode(';', $type)[0]));
// Known JSON-based media types
$jsonTypes = [
'application/json',
'application/ld+json',
'application/vnd.api+json', // JSON:API spec
'text/json', // Rare but occasionally seen
];
return in_array($type, $jsonTypes, true);
}
function req_method($supported = ['GET','PUT','POST','DELETE','HEAD','OPTIONS'],?string $method = null): string {
// Get the header from the server if not provided
$method = $method ?? ($_SERVER['REQUEST_METHOD'] ?? '');
if (!in_array($_SERVER['REQUEST_METHOD'], $supported)) {
http_response_code(405);
header('Allow: '. join(',',$supported));
exit;
}
return $method;
}
if (req_isjson()) {
$raw = file_get_contents('php://input');
$post =json_decode($raw,true,1000,JSON_INVALID_UTF8_IGNORE);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new JSONException('JSON: '.json_last_error_msg());
}
} else {
$post = $_REQUEST;
}
var_dump($_POST);
$req = (object)[
'isjson' => 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);
Loading…
Cancel
Save