forked from benjaminreid/jquery-bbq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_echo.php
More file actions
68 lines (46 loc) · 1.39 KB
/
json_echo.php
File metadata and controls
68 lines (46 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?PHP?><?
//
// Simple PHP JSON/P Echo - "Cowboy" Ben Alman - http://benalman.com/
// v1.0 - 2/20/2009
//
// Show script source if no params are passed
if (count($_GET) == 0) {
highlight_file($_SERVER['SCRIPT_FILENAME']);
die;
}
// Get request params
$obj = array();
foreach ($_GET as $key => $value) {
$obj[$key] = $value;
}
foreach ($_POST as $key => $value) {
$obj[$key] = $value;
}
$json_string = isset($obj['JSON']) ? $obj['JSON'] : null;
$jsonp_callback = isset($obj['callback']) ? $obj['callback'] : null;
// remove misc unneeded params
unset($obj['_']);
unset($obj['callback']);
unset($obj['JSON']);
$json = $json_string ? $json_string : json_encode($obj);
$jsonp = $jsonp_callback ? $jsonp_callback . "($json)" : $json;
sleep(1); // simulate slow connection :D
$is_xhr = isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
$is_referer = isset($_SERVER["HTTP_REFERER"]);
if ($is_xhr) {
header( 'Content-type: application/json' );
print $jsonp;
} else if ($is_referer) {
$params = array();
foreach ($obj as $key => $value) {
$params[] = urlencode($key) . '=' . urlencode($value);
}
$url = preg_replace('/\?.*$/', '', $_SERVER["HTTP_REFERER"]);
$url .= '?' . implode('&', $params);
header( "Location: $url");
} else {
header( 'Content-type: text/plain' );
print $jsonp;
}
?>