-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest.php
84 lines (68 loc) · 1.71 KB
/
rest.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
<?php
//echo'php is the best language';
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
//echo " Slim";
function dbConnect(){
$dbHost = "localhost";
$dbName = "forum";
$dbUser = "a";
$dbPass = "a";
$db = new PDO("mysql:host=$dbHost;dbname=$dbName", $dbUser, $dbPass);
return $db;
}
//echo "{\n";
//echo '"api":{' . "\n";
$app->post('/message/', function() {
echo "{\n";
echo '"api":{' . "\n";
$name = $_POST['name'];
$message = $_POST['comment'];
$db = dbConnect();
$insert = $db->prepare("INSERT INTO messages (name,message) VALUES ('$name','$message')");
$insert->execute();
print_r($insert);
echo '"status":"success"';
echo "}\n";
echo "}\n";
});
$app->get('/comment/', function() {
$db = dbConnect();
$select = $db->query("SELECT * FROM messages");
//echo '"name": ' . "[\n";
$output = '';
while($row = $select->fetch()) {
$output .= "<p>" . $row['name'] . " : " . $row['message'] . "</p>";
}
echo $output;
/* $db = dbConnect();
$select = $db->query("SELECT * FROM messages");
echo '"message": ' . "[\n";
$message = '';
while($row = $select->fetch()) {
$message .= $row['message'] . ',';
}
$message = rtrim($message, ",");
echo $message;
echo "]\n";*/
});
// $app->get('/messages/','getMessages');
// function getMessages(){
// $db = dbConnect();
// $select = $db->query("SELECT * FROM messages");
// $values='';
// while($row = $select->fetch()){
// $values .= '"' . $row['message'] . '"' . ',';
// }
// $values = rtrim($values,",");
// echo $values;
// echo "\n";
// }
$app->run();
//echo "}\n";
//echo "}\n";
?>