forked from andrewscofield/parse.com-php-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.php
240 lines (204 loc) · 5.68 KB
/
parse.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
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
<?
class parseRestClient{
private $appid = '';
private $restkey = '';
private $sesstoken = '';
private $parseUrl = 'https://api.parse.com/1/classes/';
/**
* When creating a new parseRestClient object
* send array with 'restkey' and 'appid'
*
*/
public function __construct($config){
if(isset($config['appid']) && isset($config['restkey'])){
$this->appid = $config['appid'];
$this->restkey = $config['restkey'];
$this->sesstoken = $config['sesstoken'];
}
else{
die('You must include your Application Id and REST Key');
}
}
/*
* 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){
$c = curl_init();
curl_setopt($c, CURLOPT_TIMEOUT, 5);
curl_setopt($c, CURLOPT_USERAGENT, 'parseRestClient/1.0');
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
if(empty($this->sesstoken)) {
curl_setopt($c, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'X-Parse-Application-Id: '.$this->appid,
'X-Parse-REST-API-Key: '.$this->restkey,
));
} else {
curl_setopt($c, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'X-Parse-Application-Id: '.$this->appid,
'X-Parse-REST-API-Key: '.$this->restkey,
'X-Parse-Session-Token: '.$this->sesstoken
));
}
curl_setopt($c, CURLOPT_CUSTOMREQUEST, $args['method']);
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['include'])){
$postData['include'] = $args['include'];
}
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);
}
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 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
* include: (optional) return multiple types of related objects in one query See: http://parse.com/docs/rest#queries-relational
*
* @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['include'])){
$params['include'] = $args['include'];
}
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'];
}
}
}
?>