1
- <?php
2
- /**
3
- Library to use parse.com rest API.
4
-
5
- Some examples on how to use:
6
-
7
- //
8
- // CREATE OBJECT
9
- //
10
-
11
- $parse = new parseRestClient(array(
12
- 'appid' => 'YOUR APPLICATION ID',
13
- 'masterkey' => 'YOUR MASTER KEY ID'
14
- ));
15
-
16
-
17
- //
18
- // CREATE EXAMPLE
19
- //
20
- $params = array(
21
- 'className' => 'gameScore',
22
- 'object' => array(
23
- 'score' => 500,
24
- 'name' => 'Andrew Scofield'
25
- )
26
- );
27
-
28
- $request = $parse->create($params);
29
-
30
-
31
- //
32
- // GET EXAMPLE
33
- //
34
-
35
- $params = array(
36
- 'className' => 'gameScore',
37
- 'objectId' => 'Ed1nuqPvcm'
38
- );
39
-
40
- $request = $parse->get($params);
41
-
42
-
43
- //
44
- //QUERY EXAMPLE
45
- //
46
-
47
- $params = array(
48
- 'className' => 'gameScore',
49
- 'query' => array(
50
- 'score'=> array(
51
- '$gt' => 500
52
- )
53
- ),
54
- 'order' => '-score',
55
- 'limit' => '2',
56
- 'skip' => '2'
57
- );
58
-
59
- $request = $parse->query($params);
60
-
61
-
62
- //
63
- // UPDATE EXAMPLE
64
- //
65
-
66
- $params = array(
67
- 'className' => 'gameScore',
68
- 'objectId' => 'Ed1nuqPvcm',
69
- 'object' => array(
70
- 'score' => 500,
71
- 'name' => 'Andrew Scofield'
72
- )
73
- );
74
-
75
- $request = $parse->update($params);
76
-
77
-
78
- //
79
- // DELETE EXAMPLE
80
- //
81
-
82
- $params = array(
83
- 'className' => 'gameScore',
84
- 'objectId' => 'Ed1nuqPvcm',
85
- );
86
-
87
- $request = $parse->delete($params);
88
-
89
-
90
- * *
91
- *
92
- */
93
-
94
-
1
+ <?
95
2
class parseRestClient{
96
3
97
4
private $ appid = '' ;
98
- private $ masterkey = '' ;
5
+ private $ restkey = '' ;
99
6
private $ parseUrl = 'https://api.parse.com/1/classes/ ' ;
100
7
101
8
102
9
/**
103
10
* When creating a new parseRestClient object
104
- * send array with 'masterkey ' and 'appid'
11
+ * send array with 'restkey ' and 'appid'
105
12
*
106
13
*/
107
14
public function __construct ($ config ){
108
- if (isset ($ config ['appid ' ]) && isset ($ config ['masterkey ' ])){
15
+ if (isset ($ config ['appid ' ]) && isset ($ config ['restkey ' ])){
109
16
$ this ->appid = $ config ['appid ' ];
110
- $ this ->masterkey = $ config ['masterkey ' ];
17
+ $ this ->restkey = $ config ['restkey ' ];
111
18
}
112
19
else {
113
20
die ('You must include your Application Id and Master Key ' );
@@ -125,13 +32,16 @@ private function request($args){
125
32
curl_setopt ($ c , CURLOPT_TIMEOUT , 5 );
126
33
curl_setopt ($ c , CURLOPT_USERAGENT , 'parseRestClient/1.0 ' );
127
34
curl_setopt ($ c , CURLOPT_RETURNTRANSFER , true );
128
- curl_setopt ($ c , CURLOPT_USERPWD , $ this ->appid . ': ' . $ this ->masterkey );
129
- curl_setopt ($ c , CURLOPT_HTTPHEADER , array ('Content-Type: application/json ' ));
35
+ curl_setopt ($ c , CURLOPT_HTTPHEADER , array (
36
+ 'Content-Type: application/json ' ,
37
+ 'X-Parse-Application-Id: ' .$ this ->appid ,
38
+ 'X-Parse-REST-API-Key: ' .$ this ->restkey
39
+ ));
130
40
curl_setopt ($ c , CURLOPT_CUSTOMREQUEST , $ args ['method ' ]);
131
41
curl_setopt ($ c , CURLOPT_URL , $ this ->parseUrl . $ args ['url ' ]);
132
42
133
43
if ($ args ['method ' ] == 'PUT ' || $ args ['method ' ] == "POST " ){
134
- $ postData = json_encode ($ args ['payload ' ], JSON_NUMERIC_CHECK );
44
+ $ postData = json_encode ($ args ['payload ' ]);
135
45
curl_setopt ($ c , CURLOPT_POSTFIELDS , $ postData );
136
46
}
137
47
else {
@@ -149,10 +59,7 @@ private function request($args){
149
59
$ postData ['skip ' ] = $ args ['skip ' ];
150
60
}
151
61
if (count ($ postData ) > 0 ){
152
- $ query = "" ;
153
- foreach ($ postData as $ key => $ value ) {
154
- $ query .= '& ' .$ key .'= ' .$ value ;
155
- }
62
+ $ query = http_build_query ($ postData , '' , '& ' );
156
63
curl_setopt ($ c , CURLOPT_URL , $ this ->parseUrl . $ args ['url ' ].'? ' .$ query );
157
64
}
158
65
@@ -298,7 +205,8 @@ public function delete($args){
298
205
private function checkResponse ($ return ,$ code ){
299
206
//TODO: Need to also check for response for a correct result from parse.com
300
207
if ($ return ['code ' ] != $ code ){
301
- die ('failed to get response ' .$ code .', response code was: ' .$ return ['code ' ]);
208
+ $ error = json_decode ($ return ['response ' ]);
209
+ die ('ERROR: response code was ' .$ return ['code ' ].' with message: ' .$ error ->error );
302
210
}
303
211
else {
304
212
return $ return ['response ' ];
0 commit comments