Skip to content

Instantly share code, notes, and snippets.

@bobbydank
Created July 2, 2023 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobbydank/f3f4c721d01a47d8cca3fdd4789e8f15 to your computer and use it in GitHub Desktop.
Save bobbydank/f3f4c721d01a47d8cca3fdd4789e8f15 to your computer and use it in GitHub Desktop.
Starting point for a PHP API Endpoint.
<?php
header("Content-Type: application/json; charset=UTF-8");
ini_set('display_errors', 0);
ini_set('log_errors', 1);
error_reporting(E_ALL);
/* Vars
-------------------------------------- */
$request = $_GET['request'];
$return = array('code' => 200, 'message' => '');
/* Check API Key
-------------------------------------- */
$api_key = 'your-test-api-key';
$headers = apache_request_headers();
if ( isset($headers['wsm-authorization']) ) {
if ( $api_key != 'your-valid-api-key' ) {
// The key is invalid, return an error response
http_response_code(401);
echo json_encode(array("message" => "Invalid API Key."));
exit();
}
}
/* Endpoints
-------------------------------------- */
switch ($request) {
case 'test':
$return = array('code' => 200, 'message' => 'test');
break;
default:
$return = array('code' => 200, 'message' => 'hello.');
break;
}
exit(json_encode($return));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment