-
Notifications
You must be signed in to change notification settings - Fork 1
/
NowDB.php
140 lines (116 loc) · 3.22 KB
/
NowDB.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
/**
* Simple Interface for NowDB.
*
* Nowdb is REST api based database. We can call it Dbaas (Database as a service)
* Nowdb use MongoDB as its engine.
*
* @author Gemblue
* @package Main
*/
class NowDB {
// Credentials
public $app_id = '54fbd4b28d909e3423aae9ff'; // Change with yours
public $token = '537ce7758d909ee80509f015'; // Change with yours
public $project = 'codepolitan_records'; // Change with yours
// API Url
public $select_url = 'http://io.nowdb.net/operation/select_all';
public $insert_url = 'http://io.nowdb.net/operation/insert';
public $update_by_id_url = 'http://io.nowdb.net/operation/update_id';
public $delete_by_id_url = 'http://io.nowdb.net/operation/delete_id';
public $select_where_like_url = 'http://io.nowdb.net/operation/select_wherelike';
// Select
public function select($table, $limit = 5)
{
$param = array(
'appid' => $this->app_id,
'token' => $this->token,
'project' => $this->project,
'collection' => $table,
'limit' => $limit
);
$operation = $this->http_request($this->select_url, $param);
return $operation;
}
// Select where like
public function select_where_like($table, $fields)
{
$param = array(
'appid' => $this->app_id,
'token' => $this->token,
'project' => $this->project,
'collection' => $table
);
$param = array_merge($fields, $param);
$operation = $this->http_request($this->select_where_like_url, $param);
return $operation;
}
// Insert
public function insert($table, $fields)
{
$param = array(
'appid' => $this->app_id,
'token' => $this->token,
'project' => $this->project,
'collection' => $table
);
$param = array_merge($fields, $param);
$operation = $this->http_request($this->insert_url, $param);
return $operation;
}
// Update by id
public function update_by_id($table, $fields)
{
$param = array(
'appid' => $this->app_id,
'token' => $this->token,
'project' => $this->project,
'collection' => $table
);
$param = array_merge($fields, $param);
$operation = $this->http_request($this->update_by_id_url, $param);
if ($operation->status == 1 && $operation->message == 'Record Updated')
{
return true;
}
else
{
return false;
}
}
// Delete by id
public function delete_by_id($table, $id)
{
$param = array(
'appid' => $this->app_id,
'token' => $this->token,
'project' => $this->project,
'collection' => $table,
'id' => $id
);
$operation = $this->http_request($this->delete_by_id_url, $param);
if ($operation->status == 1 && $operation->message == 'Record Removed')
{
return true;
}
else
{
return false;
}
}
private function http_request($url, $param)
{
$param = http_build_query($param);
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $param);
curl_setopt($curl_handle, CURLOPT_HEADER, 0);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_TIMEOUT,30);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$results = curl_exec($curl_handle);
curl_close($curl_handle);
return json_decode($results);
}
}