REST API setup
This commit is contained in:
@@ -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)];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user