Skip to content

Commit 9d9553e

Browse files
New authentication method, ,README file changes and better error messages
1 parent 9f8facd commit 9d9553e

File tree

2 files changed

+105
-187
lines changed

2 files changed

+105
-187
lines changed

README

+91-81
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,99 @@
1-
Library to use parse.com rest API.
2-
More on the api here: https://www.parse.com/docs/rest
1+
PHP parse.com API library
2+
===========================
3+
More on the parse.com api here: https://www.parse.com/docs/rest
4+
5+
Authentication Fixed
6+
-----------
7+
The authentication method was recently change, a fix has been made on this library.
8+
9+
10+
**Please note**
11+
I am working to keep up with their rapidly growing API, please submit any issues you may find. I'll work my best at keep this library up to date. If you are able to send pull requests my way!
12+
13+
314

415
Some examples on how to use this library:
516

17+
CREATE OBJECT
18+
--------------
619

7-
//
8-
// CREATE OBJECT
9-
//
10-
11-
$parse = new parseRestClient(array(
12-
'appid' => 'YOUR APPLICATION ID',
13-
'masterkey' => 'YOUR MASTER KEY ID'
14-
));
15-
20+
```
21+
$parse = new parseRestClient(array(
22+
'appid' => 'YOUR APPLICATION ID',
23+
'masterkey' => 'YOUR MASTER KEY ID'
24+
));
25+
```
1626

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);
27+
CREATE EXAMPLE
28+
----------------
2929

30+
```
31+
$params = array(
32+
'className' => 'gameScore',
33+
'object' => array(
34+
'score' => 500,
35+
'name' => 'Andrew Scofield'
36+
)
37+
);
38+
39+
$request = $parse->create($params);
40+
```
3041

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-
42+
GET EXAMPLE
43+
------------
44+
45+
```
46+
$params = array(
47+
'className' => 'gameScore',
48+
'objectId' => 'Ed1nuqPvcm'
49+
);
50+
51+
$request = $parse->get($params);
52+
```
53+
54+
QUERY EXAMPLE
55+
--------------
56+
57+
```
58+
$params = array(
59+
'className' => 'gameScore',
60+
'query' => array(
61+
'score'=> array(
62+
'$gt' => 500
63+
)
64+
),
65+
'order' => '-score',
66+
'limit' => '2',
67+
'skip' => '2'
68+
);
69+
70+
$request = $parse->query($params);
71+
```
72+
73+
UPDATE EXAMPLE
74+
---------------
75+
76+
```
77+
$params = array(
78+
'className' => 'gameScore',
79+
'objectId' => 'Ed1nuqPvcm',
80+
'object' => array(
81+
'score' => 500,
82+
'name' => 'Andrew Scofield'
83+
)
84+
);
85+
86+
$request = $parse->update($params);
87+
```
88+
89+
DELETE EXAMPLE
90+
----------------
91+
92+
```
93+
$params = array(
94+
'className' => 'gameScore',
95+
'objectId' => 'Ed1nuqPvcm',
96+
);
8997

98+
$request = $parse->delete($params);
99+
```

parse.php

+14-106
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,20 @@
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+
<?
952
class parseRestClient{
963

974
private $appid = '';
98-
private $masterkey = '';
5+
private $restkey = '';
996
private $parseUrl = 'https://api.parse.com/1/classes/';
1007

1018

1029
/**
10310
* When creating a new parseRestClient object
104-
* send array with 'masterkey' and 'appid'
11+
* send array with 'restkey' and 'appid'
10512
*
10613
*/
10714
public function __construct($config){
108-
if(isset($config['appid']) && isset($config['masterkey'])){
15+
if(isset($config['appid']) && isset($config['restkey'])){
10916
$this->appid = $config['appid'];
110-
$this->masterkey = $config['masterkey'];
17+
$this->restkey = $config['restkey'];
11118
}
11219
else{
11320
die('You must include your Application Id and Master Key');
@@ -125,13 +32,16 @@ private function request($args){
12532
curl_setopt($c, CURLOPT_TIMEOUT, 5);
12633
curl_setopt($c, CURLOPT_USERAGENT, 'parseRestClient/1.0');
12734
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+
));
13040
curl_setopt($c, CURLOPT_CUSTOMREQUEST, $args['method']);
13141
curl_setopt($c, CURLOPT_URL, $this->parseUrl . $args['url']);
13242

13343
if($args['method'] == 'PUT' || $args['method'] == "POST"){
134-
$postData = json_encode($args['payload'], JSON_NUMERIC_CHECK);
44+
$postData = json_encode($args['payload']);
13545
curl_setopt($c, CURLOPT_POSTFIELDS, $postData );
13646
}
13747
else{
@@ -149,10 +59,7 @@ private function request($args){
14959
$postData['skip'] = $args['skip'];
15060
}
15161
if(count($postData) > 0){
152-
$query = "";
153-
foreach ($postData as $key => $value) {
154-
$query .= '&'.$key.'='.$value;
155-
}
62+
$query = http_build_query($postData, '', '&');
15663
curl_setopt($c, CURLOPT_URL, $this->parseUrl . $args['url'].'?'.$query);
15764
}
15865

@@ -298,7 +205,8 @@ public function delete($args){
298205
private function checkResponse($return,$code){
299206
//TODO: Need to also check for response for a correct result from parse.com
300207
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);
302210
}
303211
else{
304212
return $return['response'];

0 commit comments

Comments
 (0)