Skip to content

Instantly share code, notes, and snippets.

@mladoux
Created August 30, 2020 07:22
Show Gist options
  • Save mladoux/21b5d71fced3d47bc28a3d8f86a3d9e1 to your computer and use it in GitHub Desktop.
Save mladoux/21b5d71fced3d47bc28a3d8f86a3d9e1 to your computer and use it in GitHub Desktop.
takes data from pan-player-analytics/Plan and strips out unwanted elements for public presentation.
<?php
// API endpoint
$api = 'example.com/api_call';
// Parse uri segments
function uri(int $n)
{
$n++;
$parts = explode('/', $_SERVER['REQUEST_URI']);
return (isset($parts[$n])) ? trim($parts[$n]) : null;
}
// Recursively strip unwanted keys from array
function removeRecursive($haystack,$needle){
if(is_array($haystack)) {
if (isset($haystack[$needle])) {
unset($haystack[$needle]);
}
foreach ($haystack as $k=>$value) {
$haystack[$k] = removeRecursive($value,$needle);
}
}
return $haystack;
}
// generate 404 message
function show404()
{
$response = [
'error' => 404,
'message' => 'Data not found',
];
return json_encode($response);
}
// generate player message results
function player()
{
$uuid = uri(1);
$response = null;
$badKeys = [
'gm_series',
'sessions',
];
global $api;
if($uuid !== null) {
$responseArray = json_decode(file_get_contents($api . 'player?player=' . $uuid), true);
foreach ($badKeys as $key) {
$responseArray = removeRecursive($responseArray, $key);
}
$response = json_encode($responseArray);
}
return $response;
}
// Get action segment
$action = uri(0);
// determine what action to process
switch($action) {
case 'player':
$response = player();
break;
default:
$response = null;
}
// if no response set, set 404 response message
if ($response === null || $response == 'null') {
header("HTTP/1.1 404 Not Found");
$response = show404();
}
header('Content-Type: application/json');
// return the response
echo $response;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment