Skip to content

Instantly share code, notes, and snippets.

@remino
Created January 6, 2016 08:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remino/ccb7e370731aff96202c to your computer and use it in GitHub Desktop.
Save remino/ccb7e370731aff96202c to your computer and use it in GitHub Desktop.
Return browser headers in JSON
<?php
class JsonHttpHeaders {
function headers() {
$headers = '';
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$header_name =
str_replace(' ', '-',
ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))));
$headers[$header_name] = $value;
}
}
return $headers;
}
function get_json_headers() {
return json_encode($this->headers());
}
function json_headers() {
header('Content-Type: application/json');
echo $this->get_json_headers();
}
function json_headers_callback($callback) {
header('Content-Type: application/javascript');
echo "{$callback}({$this->get_json_headers()});";
}
function run($callback = null) {
if($callback) { $this->json_headers_callback($callback); }
else { $this->json_headers(); }
}
}
$json_http_headers = new JsonHttpHeaders();
$json_http_headers->run($_GET['callback']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment