-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.php
More file actions
55 lines (47 loc) · 1.71 KB
/
index.php
File metadata and controls
55 lines (47 loc) · 1.71 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
<?php
// My microservice!
if(!defined('STDOUT')) define('STDOUT', fopen('php://stdout', 'w'));
fwrite(STDOUT, 'Fighter is running');
set_exception_handler(function ($e) {
$code = $e->getCode() ? : 400;
header("Content-Type: application/json", NULL, $code);
echo json_encode(["error" => $e->getMessage()]);
exit;
});
// assume JSON, handle requests by verb and path
$verb = $_SERVER['REQUEST_METHOD'];
// declare GET request response and database query response
$getRequestResponse;
$dbQueryResponse;
error_log(print_r($verb, TRUE));
switch ($verb) {
case 'GET':
// sample GET URL
$url = "http://ip.jsontest.com/";
$getRequestResponse = file_get_contents($url);
$link = mysqli_connect('myHost', 'myUser', 'myPassword', 'myDatabase');
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
echo 'Connected successfully';
$query = 'SELECT * FROM MyTable';
$result = mysqli_query($link, $query);
$dbQueryResponse = $result->fetch_assoc();
}
break;
default:
throw new Exception('Method Not Supported', 405);
}
header("Content-Type: application/json");
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:GET, POST, OPTIONS, PUT, PATCH, DELETE');
header('Access-Control-Allow-Headers:X-Requested-With,content-type');
header('Access-Control-Allow-Credentials:true');
echo $getRequestResponse;
echo 'X: ' . $dbQueryResponse['xCoordinate'];
echo 'Y: ' . $dbQueryResponse['yCoordinate'];
fwrite(STDOUT, $getRequestResponse);
fwrite(STDOUT, 'X: ' . $dbQueryResponse['xCoordinate']);
fwrite(STDOUT, 'Y: ' . $dbQueryResponse['yCoordinate']);
fclose(STDOUT);