-
Notifications
You must be signed in to change notification settings - Fork 10
/
BringApi.php
211 lines (193 loc) · 6.53 KB
/
BringApi.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
<?php
/*
* Incomplete, reverse-engineered and unofficial Bring! API
*/
class BringApi {
const GET_REQUEST = 'get';
const POST_REQUEST = 'post';
const PUT_REQUEST = 'put';
private $bringRestURL = "https://api.getbring.com/rest/";
private $bringUUID = "";
private $bringListUUID = "";
private $answerHttpStatus = -1;
/**
* It is recommended to use the UUID/listUUID method to save one request to the getbring server
*
* @param string $UUID Should contain a UUID or the email of the user
* @param string $listUUID Should contain a listUUID or a password of the user
* @param bool $useLogin true if you want to use a email/password login combination
*/
public function __construct($UUID,$listUUID, $useLogin = false)
{
if($useLogin) {
$login = json_decode($this->login($UUID,$listUUID),true);
if($this->answerHttpStatus == 200 && $login != "") {
$this->bringUUID = $login['uuid'];
$this->bringListUUID = $login['bringListUUID'];
} else {
die("Wrong Login!");
}
} else {
$this->bringUUID = $UUID;
$this->bringListUUID = $listUUID;
}
}
/**
* @param string $email
* @param string $password
*/
private function login($email,$password)
{
return $this->request(self::GET_REQUEST,"bringlists","?email=".$email."&password=".$password);
}
/**
* Get all items from the current selected shopping list
*
* @return json string or html code
*/
public function getItems()
{
return $this->request(self::GET_REQUEST,"bringlists/".$this->bringListUUID,"",true);
}
/**
* Save an item to your current shopping list
*
* @param string $itemName The name of the item you want to send to the bring server
* @param string $specification The litte description under the name of the item
* @return should return an empty string and $answerHttpStatus should contain 204. If not -> error
*/
public function saveItem($itemName,$specification)
{
return $this->request(self::PUT_REQUEST,"bringlists/".$this->bringListUUID,"purchase=".$itemName."&recently=&specification=".$specification."&remove=&sender=null",true);
}
/**
* remove an item from your current shopping list
*
* @param string $itemName Name of the item you want to delete from you shopping list
* @return should return an empty string and $answerHttpStatus should contain 204. If not -> error
*/
public function removeItem($itemName)
{
return $this->request(self::PUT_REQUEST,"bringlists/".$this->bringListUUID, "purchase=&recently=&specification=&remove=".$itemName."&sender=null",true);
}
/**
* Search for an item
*
* @param string $search The item you want to search
* @return json string or html code
*/
public function searchItem($search)
{
return $this->request(self::GET_REQUEST,"bringlistitemdetails/", "?listUuid=".$this->bringListUUID."&itemId=".$search,true);
}
// Hidden Icons? Don't know what this is used for
public function loadProducts()
{
return $this->request(self::GET_REQUEST,"bringproducts", "",true);
}
// Found Icons? Don't know what this is used for
public function loadFeatures()
{
return $this->request(self::GET_REQUEST,"bringusers/".$this->bringUUID."/features", "",true);
}
/**
* Loads all shopping lists
*
* @return json string or html code
*/
public function loadLists()
{
return $this->request(self::GET_REQUEST,"bringusers/".$this->bringUUID."/lists", "",true);
}
/**
* Get all users from a shopping list
*
* @param string $listUUID The lisUUID you want to recive a list of users from.
* @return json string or html code
*/
public function getAllUsersFromList($listUUID)
{
return $this->request(self::GET_REQUEST,"bringlists/".$listUUID."/users", "",true);
}
/**
* @return json string or html code
*/
public function getUserSettings()
{
return $this->request(self::GET_REQUEST,"bringusersettings/".$this->bringUUID, "",true);
}
/**
* @return int containing the http status code from the answer
*/
public function getHttpStatus()
{
return $this->answerHttpStatus;
}
/**
* Handles the request to the server
*
* @param const string $type The HTTP request type.
* @param string $request contains the request URL
* @param string $parameter The parameters we send with the request
* @param bool $customHeader True if you want to send the custom header (That is necessary because it sends the API-KEY) with the request
* @return The answer string from the server
*/
private function request($type = self::GET_REQUEST,$request, $parameter, $customHeader = false)
{
$ch = curl_init();
$additionalHeaderInfo = "";
switch($type) {
case self::GET_REQUEST:
curl_setopt($ch, CURLOPT_URL, $this->bringRestURL.$request.$parameter);
break;
case self::POST_REQUEST:
curl_setopt($ch, CURLOPT_URL, $this->bringRestURL.$request);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$parameter);
break;
case self::PUT_REQUEST:
$fh = tmpfile();
fwrite($fh, $parameter);
fseek($fh, 0);
curl_setopt($ch, CURLOPT_URL, $this->bringRestURL.$request);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_INFILE, $fh);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($parameter));
$additionalHeaderInfo = 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8';
break;
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if($customHeader) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHeader(($additionalHeaderInfo != "")?$additionalHeaderInfo:null));
}
$server_output = curl_exec ($ch);
$this->answerHttpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close ($ch);
return $server_output;
}
/**
* @param string|array $additional additional field that you want to add to the header
* @return array with the headerinformation
*/
private function getHeader($additional = null)
{
$header = [
'X-BRING-API-KEY: cof4Nc6D8saplXjE3h3HXqHH8m7VU2i1Gs0g85Sp',
'X-BRING-CLIENT: android',
'X-BRING-USER-UUID: '.$this->bringUUID,
'X-BRING-VERSION: 303070050',
'X-BRING-COUNTRY: de',
];
if($additional != null) {
if(is_array($additional)) {
foreach($additional as $key => $value) {
$header[] = $value;
}
} else {
$header[] = $additional;
}
}
return $header;
}
}
?>