forked from andrewscofield/parse.com-php-library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.php
More file actions
265 lines (233 loc) · 6.69 KB
/
parse.php
File metadata and controls
265 lines (233 loc) · 6.69 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
class parseRestClient
{
const API_BASE_URL = "https://api.parse.com/1";
private $appid;
private $restkey;
private $masterkey;
private $parseUrl;
private $pushUrl;
/**
* When creating a new parseRestClient object
* send array with 'masterkey', 'restkey', and 'appid'
*
*/
public function __construct($config)
{
if (isset($config['appid']) && (isset($config['masterkey']) || isset($config['restkey']))) {
$this->appid = $config['appid'];
$this->restkey = $config['restkey'];
$this->masterkey = $config['masterkey'];
} else {
die('You must include your Application Id and Master Key');
}
$this->parseUrl = self::API_BASE_URL . '/classes/';
$this->pushUrl = self::API_BASE_URL . '/';
}
/**
* All requests go through this function
* There are functions that filter all the different request types
* No need to use this function directly
*
*/
private function request($args)
{
$httpHeaders = array(
'Content-Type: application/json',
'X-Parse-Application-Id: ' . $this->appid,
);
// @see https://www.parse.com/docs/rest#users-security
$httpHeaders[] = $this->masterkey ? "X-Parse-Master-Key: " . $this->masterkey :
"X-Parse-REST-API-Key: " . $this->restkey;
$c = curl_init();
curl_setopt($c, CURLOPT_TIMEOUT, 30);
curl_setopt($c, CURLOPT_USERAGENT, 'parseRestClient/1.0');
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_HTTPHEADER, $httpHeaders);
curl_setopt($c, CURLOPT_CUSTOMREQUEST, $args['method']);
if ($args['url'] == "push") {
curl_setopt($c, CURLOPT_URL, $this->pushUrl . $args['url']);
} else {
curl_setopt($c, CURLOPT_URL, $this->parseUrl . $args['url']);
}
if ($args['method'] == 'PUT' || $args['method'] == "POST") {
$postData = json_encode($args['payload']);
curl_setopt($c, CURLOPT_POSTFIELDS, $postData);
} else {
$postData = array();
if (isset($args['query'])) {
$postData['where'] = json_encode($args['query']);
}
if (isset($args['order'])) {
$postData['order'] = $args['order'];
}
if (isset($args['limit'])) {
$postData['limit'] = $args['limit'];
}
if (isset($args['skip'])) {
$postData['skip'] = $args['skip'];
}
if (count($postData) > 0) {
$query = http_build_query($postData, '', '&');
curl_setopt($c, CURLOPT_URL, $this->parseUrl . $args['url'] . '?' . $query);
}
}
$response = curl_exec($c);
$httpCode = curl_getinfo($c, CURLINFO_HTTP_CODE);
return array('code' => $httpCode, 'response' => $response);
}
/**
* Used to create a parse.com object
*
* @param array $args - argument hash:
*
* className: string of className
* object: object to create
*
* @return string $return
*
*/
public function create($args)
{
$params = array(
'url' => $args['className'],
'method' => 'POST',
'payload' => $args['object']
);
$return = $this->request($params);
return $this->checkResponse($return, '201');
}
/**
* Used to send a push notification
*
* @param array $args - argument hash:
*
* push: leave this alone
* object: notification details
*
* @return string $return
*
*/
public function notification($args)
{
$params = array(
'url' => 'push',
'method' => 'POST',
'payload' => $args['object']
);
$return = $this->request($params);
return $this->checkResponse($return, '201');
}
/**
* Used to get a parse.com object
*
* @param array $args - argument hash:
*
* className: string of className
* objectId: (optional) the objectId of the object you want to update. If none, will return multiple objects from className
*
* @return string $return
*
*/
public function get($args)
{
$params = array(
'url' => $args['className'] . '/' . $args['objectId'],
'method' => 'GET'
);
$return = $this->request($params);
return $this->checkResponse($return, '200');
}
/*
* Used to update a parse.com object
*
* @param array $args - argument hash:
*
* className: string of className
* objectId: the objectId of the object you want to update
* object: object to update in place of old one
*
* @return string $return
*
*/
public function update($args)
{
$params = array(
'url' => $args['className'] . '/' . $args['objectId'],
'method' => 'PUT',
'payload' => $args['object']
);
$return = $this->request($params);
return $this->checkResponse($return, '200');
}
/**
* Used to query parse.com.
*
* @param array $args - argument hash:
*
* className: string of className
* query: array containing query. See: https://www.parse.com/docs/rest#data-querying
* order: (optional) used to sort by the field name. use a minus (-) before field name to reverse sort
* limit: (optional) limit number of results
* skip: (optional) used to paginate results
*
* @return string $return
*
*/
public function query($args)
{
$params = array(
'url' => $args['className'],
'method' => 'GET'
);
if (isset($args['query'])) {
$params['query'] = $args['query'];
}
if (isset($args['order'])) {
$params['order'] = $args['order'];
}
if (isset($args['limit'])) {
$params['limit'] = $args['limit'];
}
if (isset($args['skip'])) {
$params['skip'] = $args['skip'];
}
$return = $this->request($params);
return $this->checkResponse($return, '200');
}
/**
* Used to get a parse.com object
*
* @param array $args - argument hash:
*
* className: string of className
* objectId: (optional) the objectId of the object you want to update. If none, will return multiple objects from className
*
* @return string $return
*/
public function delete($args)
{
$params = array(
'url' => $args['className'] . '/' . $args['objectId'],
'method' => 'DELETE'
);
$return = $this->request($params);
return $this->checkResponse($return, '200');
}
/**
* Checks for correct/expected response code.
*
* @param array $return, string $code
* @return string $return['response]
*/
private function checkResponse($return, $code)
{
//TODO: Need to also check for response for a correct result from parse.com
if ($return['code'] != $code) {
$error = json_decode($return['response']);
die('ERROR: response code was ' . $return['code'] . ' with message: ' . $error->error);
} else {
return $return['response'];
}
}
}