-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjsonp.php
executable file
·43 lines (38 loc) · 1.15 KB
/
jsonp.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
<?php
/**
* Set the callback variable to what jQuery sends over
*/
$callback = (string)$_GET['callback'];
if (!$callback) $callback = 'callback';
/**
* The $filename parameter determines what file to load from local source
*/
$filename = $_GET['filename'];
if (preg_match('/^[a-zA-Z]+[a-zA-Z\d\-\/\_\.]+\.json$/', $filename)) {
$json = file_get_contents($filename);
}
if (preg_match('/^[a-zA-Z]+[a-zA-Z\d\-\/\_\.]+\.csv$/', $filename)) {
$csv = str_replace('"', '\"', file_get_contents($filename));
$csv = preg_replace( "/[\r\n]+/", "\\n", $csv);
$json = '"' . $csv . '"';
}
/**
* The $url parameter loads data from external sources
*/
@$url = $_GET['url'];
if ($url) {
if (preg_match('/^(http|https):\/\/[\w\W]*\.xml$/', $url)) {
$xml = simplexml_load_file($url);
$json = json_encode($xml);
} else if (preg_match('/^(http|https):\/\/[\/\w \.-]*\.csv$/', $url)) {
$csv = str_getcsv(file_get_contents($url));
$json = json_encode($xml);
// Assume JSON
} else if (preg_match('/^(http|https):\/\/[\/\w \.-]*$/', $url)) {
$json = file_get_contents($url);
}
}
// Send the output
header('Content-Type: text/javascript');
echo "$callback($json);";
?>