-
Notifications
You must be signed in to change notification settings - Fork 4
/
echo.php
106 lines (95 loc) · 3.21 KB
/
echo.php
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/*
* Echo test webservice - respond with request and header details
*
* http://scooterlabs.com/echo - plain text
* http://scooterlabs.com/echo.json - JSON format
* http://scooterlabs.com/echo.xml - XML format
* http://scooterlabs.com/echo?ip - return just client IP address (text)
*
* Brian Cantoni <brian AT cantoni.org>
* https://github.com/bcantoni/echotest
*/
if (function_exists('getallheaders')) {
$all_headers = getallheaders();
} else {
$all_headers = array();
}
// remove any Google Adsense cookies to clean up the output
removeGoogleAdsense ();
$data = array (
'method' => $_SERVER['REQUEST_METHOD'],
'headers' => $all_headers,
'request' => $_REQUEST,
// 'server' => $_REQUEST, // only advisable for debug, don't make public
'client_ip' => get_ip_address (),
'time_utc' => gmdate (DATE_ISO8601),
'info' => 'Echo service from Scooterlabs (http://www.scooterlabs.com)',
);
$uri = $_SERVER['REQUEST_URI'];
if (isset($_REQUEST['ip'])) {
header ("Content-type: text/plain");
print $data['client_ip'];
} else if (preg_match ('/^\/echo.json/', $uri)) {
header ("Content-type: application/json");
print json_encode ($data) . "\n";
} else if (preg_match ('/^\/echo.xml/', $uri)) {
header ("Content-type: application/xml");
print array_to_xml ($data, new SimpleXMLElement ('<echo/>'))->asXML();
} else {
header ("Content-type: text/plain");
print <<<EOT
Simple webservice echo test: make a request to this endpoint to return the HTTP request parameters and headers. Results available in plain text, JSON, or XML formats. See http://www.cantoni.org/2012/01/08/simple-webservice-echo-test for more details, or https://github.com/bcantoni/echotest for source code.
EOT;
print_r ($data);
}
/*
* Recursively convert PHP array to XML string
*
* modified from original: http://stackoverflow.com/a/3289602/9965
*/
function array_to_xml (array $arr, SimpleXMLElement $xml) {
foreach ($arr as $k => $v) {
is_array ($v)
? array_to_xml ($v, $xml->addChild ($k))
: $xml->addChild ($k, xmlEscape ($v));
}
return $xml;
}
/*
* Escape special XML chars
*
*/
function xmlEscape($string) {
return str_replace(array('&', '<', '>', '\'', '"'), array('&', '<', '>', ''', '"'), $string);
}
/*
* Remove Google Adsense cookies if present in global $_REQUEST
*
*/
function removeGoogleAdsense () {
if ($_REQUEST) {
foreach ($_REQUEST as $k=>$v) {
if (preg_match ('/^__utm/', $k)) {
unset ($_REQUEST[$k]);
}
}
}
}
/*
* Return best match for client IP address
*
* source: http://www.kavoir.com/2010/03/php-how-to-detect-get-the-real-client-ip-address-of-website-visitors.html
*/
function get_ip_address() {
foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
if (array_key_exists($key, $_SERVER) === true) {
foreach (explode(',', $_SERVER[$key]) as $ip) {
if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
return $ip;
}
}
}
}
return ('');
}