Compare commits
2 Commits
main
...
e4fac4c6b4
| Author | SHA1 | Date |
|---|---|---|
|
|
e4fac4c6b4 | 9 months ago |
|
|
f9eb6c4095 | 9 months ago |
@ -0,0 +1,2 @@ |
||||
*~ |
||||
node_modules/ |
||||
@ -1,8 +0,0 @@ |
||||
ISC License: |
||||
|
||||
Copyright (c) 2004-2010 by Internet Systems Consortium, Inc. ("ISC") |
||||
Copyright (c) 1995-2003 by Internet Software Consortium |
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
||||
@ -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…
Reference in new issue