-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
42 lines (33 loc) · 907 Bytes
/
index.php
File metadata and controls
42 lines (33 loc) · 907 Bytes
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
<?php
set_error_handler('error_handler');
ini_set('display_errors', 'off');
error_reporting( E_ALL );
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
$output = array();
$conn = pg_connect(pg_connect_string());
$q = pg_query($conn, $_GET['q']);
function return_error($message, $conn) {
$output = array();
$output['error'] = $message;
pg_close($conn);
http_response_code(406);
exit(json_encode($output));
}
function error_handler($errno, $errstr, $errfile, $errline) {
return_error($errstr, $conn);
}
function pg_connect_string() {
extract(parse_url($_ENV["DATABASE_URL"]));
return "user=$user password=$pass host=$host dbname=" . substr($path, 1) ." sslmode=require";
}
if (!$q) {
return_error(pg_last_error($conn), $conn);
}
else {
while ($row = pg_fetch_object($q)) {
$output[] = $row;
}
}
pg_close($conn);
exit(json_encode($output));