-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.php
71 lines (61 loc) · 2.29 KB
/
index.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
<?php require 'vendor/autoload.php';
// Create a new Container
$container = new \Slim\Container([
// Add Guzzle as 'http'
'http' => function () {
return new GuzzleHttp\Client();
},
// Add mysqli as 'mysql'
'mysql' => function () {
$mysqli = new mysqli(
getenv('DATABASE_HOST'),
getenv('DATABASE_USER'),
getenv('DATABASE_PASSWORD'),
getenv('DATABASE_NAME')
);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli->connect_error;
exit;
} else {
return $mysqli;
}
},
]);
// Instantiate the App object
$app = new \Slim\App($container);
// Get weather by location ID
$app->get('/locations/{id}', function ($request, $response, $args) {
// Get the location from the database
$id = $this->mysql->real_escape_string($args['id']);
$results = $this->mysql->query("SELECT * FROM locations WHERE id='{$id}'");
// If location found, then show it from the DB, otherwise, query MetaWeather
if ($results->num_rows > 0) {
$result = $results->fetch_assoc()['weather'];
} else {
$result = $this->http->get("https://www.metaweather.com/api/location/{$id}")
->getBody()
->getContents();
$cleanResult = $this->mysql->real_escape_string($result);
if (!$this->mysql->query("INSERT into locations (id, weather) VALUES ('{$id}', '{$cleanResult}')")) {
throw new Exception("Location could not be updated.");
}
}
// Return the results as JSON
return $response->withStatus(200)->withJson(json_decode($result));
});
$app->delete('/locations/{id}', function ($request, $response, $args) {
// Get the location from the database
$id = $this->mysql->real_escape_string($args['id']);
$results = $this->mysql->query("SELECT * FROM locations WHERE id='{$id}'");
// If it exists, delete it, otherwise send a 404
if (
$results->num_rows > 0 &&
$this->mysql->query("DELETE FROM locations WHERE id='{$id}'")
) {
return $response->withStatus(200)->write("Location {$args['id']} deleted.");
} else {
return $response->withStatus(404)->write("Location {$args['id']} not found.");
}
});
// Run the application
$app->run();