REST API setup

This commit is contained in:
2025-05-02 12:25:35 -06:00
parent 3a11453dd1
commit f9eb6c4095
4 changed files with 143 additions and 0 deletions
+55
View File
@@ -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)];
}
}
}