forked from andrewscofield/parse.com-php-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.php
215 lines (188 loc) · 5.18 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
<?php
include 'parseConfig.php';
include 'parseObject.php';
include 'parseQuery.php';
include 'parseUser.php';
include 'parseFile.php';
include 'parsePush.php';
include 'parseGeoPoint.php';
include 'parseACL.php';
class parseRestClient{
private $_appid = '';
private $_masterkey = '';
private $_restkey = '';
private $_parseurl = '';
public $data;
public $requestUrl = '';
public $returnData = '';
public function __construct(){
$parseConfig = new parseConfig;
$this->_appid = $parseConfig::APPID;
$this->_masterkey = $parseConfig::MASTERKEY;
$this->_restkey = $parseConfig::RESTKEY;
$this->_parseurl = $parseConfig::PARSEURL;
if(empty($this->_appid) || empty($this->_restkey) || empty($this->_masterkey)){
$this->throwError('You must set your Application ID, Master Key and REST API Key');
}
$version = curl_version();
$ssl_supported = ( $version['features'] & CURL_VERSION_SSL );
if(!$ssl_supported){
$this->throwError('CURL ssl support not found');
}
}
/*
* All requests go through this function
*
*
*/
public function request($args){
$isFile = false;
$c = curl_init();
curl_setopt($c, CURLOPT_TIMEOUT, 30);
curl_setopt($c, CURLOPT_USERAGENT, 'parse.com-php-library/2.0');
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLINFO_HEADER_OUT, true);
if(substr($args['requestUrl'],0,5) == 'files'){
curl_setopt($c, CURLOPT_HTTPHEADER, array(
'Content-Type: '.$args['contentType'],
'X-Parse-Application-Id: '.$this->_appid,
'X-Parse-Master-Key: '.$this->_masterkey
));
$isFile = true;
}
else if(substr($args['requestUrl'],0,5) == 'users' && isset($args['sessionToken'])){
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: '.$args['sessionToken']
));
}
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-Master-Key: '.$this->_masterkey
));
}
curl_setopt($c, CURLOPT_CUSTOMREQUEST, $args['method']);
$url = $this->_parseurl . $args['requestUrl'];
if($args['method'] == 'PUT' || $args['method'] == 'POST'){
if($isFile){
$postData = $args['data'];
}
else{
$postData = json_encode($args['data']);
}
curl_setopt($c, CURLOPT_POSTFIELDS, $postData );
}
if($args['requestUrl'] == 'login'){
$urlParams = http_build_query($args['data'], '', '&');
$url = $url.'?'.$urlParams;
}
if(array_key_exists('urlParams',$args)){
$urlParams = http_build_query($args['urlParams'], '', '&');
$url = $url.'?'.$urlParams;
}
curl_setopt($c, CURLOPT_URL, $url);
$response = curl_exec($c);
$responseCode = curl_getinfo($c, CURLINFO_HTTP_CODE);
$expectedCode = '200';
if($args['method'] == 'POST' && substr($args['requestUrl'],0,4) != 'push'){
$expectedCode = '201';
}
if($expectedCode != $responseCode){
//BELOW HELPS WITH DEBUGGING
//print_r($response);
//print_r($args);
}
return $this->checkResponse($response,$responseCode,$expectedCode);
}
public function dataType($type,$params){
if($type != ''){
switch($type){
case 'date':
$return = array(
"__type" => "Date",
"iso" => date("c", strtotime($params))
);
break;
case 'bytes':
$return = array(
"__type" => "Bytes",
"base64" => base64_encode($params)
);
break;
case 'pointer':
$return = array(
"__type" => "Pointer",
"className" => $params[0],
"objectId" => $params[1]
);
break;
case 'geopoint':
$return = array(
"__type" => "GeoPoint",
"latitude" => floatval($params[0]),
"longitude" => floatval($params[1])
);
break;
case 'file':
$return = array(
"__type" => "File",
"name" => $params[0],
);
break;
case 'increment':
$return = array(
"__op" => "Increment",
"amount" => $params[0]
);
break;
case 'decrement':
$return = array(
"__op" => "Decrement",
"amount" => $params[0]
);
break;
default:
$return = false;
break;
}
return $return;
}
}
public function throwError($msg,$code=0){
throw new ParseLibraryException($msg,$code);
}
private function checkResponse($response,$responseCode,$expectedCode){
//TODO: Need to also check for response for a correct result from parse.com
if($responseCode != $expectedCode){
$error = json_decode($response);
$this->throwError($error->error,$error->code);
}
else{
//check for empty return
if($response == '{}'){
return true;
}
else{
return json_decode($response);
}
}
}
}
class ParseLibraryException extends Exception{
public function __construct($message, $code = 0, Exception $previous = null) {
//codes are only set by a parse.com error
if($code != 0){
$message = "parse.com error: ".$message;
}
parent::__construct($message, $code, $previous);
}
public function __toString() {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
}
?>