Created
January 6, 2016 08:45
-
-
Save remino/ccb7e370731aff96202c to your computer and use it in GitHub Desktop.
Return browser headers in JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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