-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
140 lines (121 loc) · 3.93 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
error_reporting(E_ALL);
try {
include __DIR__ . "/services.php";
require_once 'ShortOID.php';
$app = new \Phalcon\Mvc\Micro($di);
$app->get('/', function(){
//phpinfo();
$mid = new MongoId();
echo 'mid= ' . $mid . '</br>';
$fid = ShortOID::fixMongoId($mid);
echo 'fid = ' . $fid . '</br>';
$sid = ShortOID::encode($mid);
echo 'sid = ' . $sid . '</br>';
$rid = ShortOID::decode($sid);
echo 'rid = ' . $rid . '</br>';
});
/*
Create a new object
*/
$app->post('/objects/{name}', function($name){
// 数据保存在 $GLOBALS['HTTP_RAW_POST_DATA'] 中
// http请求必须设置 Content-Type: application/json
$json = $GLOBALS['HTTP_RAW_POST_DATA'];
$data = json_decode($json, true);
ShortOID::revert($data, true);
try{
$collection = (new MumuObject())->getConnection()->selectCollection($name);
$collection->insert($data);
echo 'success';
}catch (\Exception $e){
echo 'faild';
}
});
/*
Updating Objects
*/
$app->put('/objects/{name}/{id}', function($name, $id){
// put数据使用 file_get_contents("php://input") 获取
// http请求必须设置 Content-Type: application/json
$json = file_get_contents("php://input");
$data = json_decode($json);
if(ShortOID::revert($data, false)){
$query = array('_id' => $data['_id']);
}else{
$objectId = ShortOID::decode($id);
$query = array('_id' => new MongoId($objectId));
}
try{
$collection = (new MumuObject())->getConnection()->selectCollection($name);
$newData = array('$set' => $data);
$result = $collection->update($query, $newData);
echo 'success';
}catch (\Exception $e){
echo 'faild';
}
});
/*
Retrieving Objects
*/
$app->get('/objects/{name}/{id}', function($name, $id){
$objectId = ShortOID::decode($id);
try{
$collection = (new MumuObject())->getConnection()->selectCollection($name);
$query = array('_id' => new MongoId($objectId));
$result = $collection->findOne($query);
ShortOID::replace($result);
echo json_encode($result);
}catch (\Exception $e){
echo 'faild';
}
});
/*
Queries
*/
$app->get('/objects/{name}', function($name){
// get参数为 where={json},并使用urlencode处理
try{
$collection = (new MumuObject())->getConnection()->selectCollection($name);
$query = $_GET['where'];
$cursor = $collection->find(json_decode($query));
$result = '[';
foreach ($cursor as $doc) {
ShortOID::replace($doc);
$result .= json_encode($doc);
if($cursor->hasNext()){
$result .= ',';
}
}
$result .= ']';
echo $result;
}catch (\Exception $e){
echo 'faild';
}
});
/*
Deleting Objects
*/
$app->delete('/objects/{name}/{id}', function($name, $id){
$objectId = ShortOID::decode($id);
try{
$collection = (new MumuObject())->getConnection()->selectCollection($name);
$query = array('_id' => new MongoId($objectId));
$result = $collection->remove($query);
echo 'success';
}catch (\Exception $e){
echo 'faild';
}
});
/*
404, not found
*/
$app->notFound(function () use ($app) {
$app->response->setStatusCode(404, "Not Found")->sendHeaders();
echo 'This is crazy, but this page was not found!';
echo '<br><br>';
});
$app->handle();
} catch (\Exception $e) {
echo $e->getMessage();
}