-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickbase_rest_api_sdk.php
More file actions
297 lines (259 loc) · 9.75 KB
/
quickbase_rest_api_sdk.php
File metadata and controls
297 lines (259 loc) · 9.75 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
/*
Title : QuickBase 2022 PHP REST API SDK
Author : Joseph Harburg (josephharburg@gmail.com)
Description : The QuickBase PHP SDK is a class for very basic interaction with the QuickBase REST API in 2022.
The QuickBase REST API is documented here:
https://developer.quickbase.com/
*/
// ini_set('display_errors', 'on'); // ini setting for turning on errors
Class QuickBaseRestApi {
public $user_token = ''; // Valid user token
public $app_token = ''; //Valid app token. Required.
public $base_url = "https://api.quickbase.com/v1"; //The current base url
public $realm = ''; //Quickbase realm string BEFORE .quickbase.com
public $user_agent = ''; //User agent
public function __construct($user_token='', $app_token = '', $realm = '', $user_agent = '', $access_token = '') {
if($user_token) $this->user_token = $user_token;
if($app_token) $this->app_token = $app_token;
if($realm) $this->realm = $realm . '.quickbase.com';
if($user_agent) $this->user_token = $user_token;
}
/**
* Method to make the request to QuickBase API
*
* @param string $type_of_request The type of http request
* @param string $endpoint The enpoint to request. Required
* @param string $body The correctly formatted data for posting. Optional unless Using POST
*
* @return mixed $response
*/
//See https://developer.quickbase.com/ for actions and endpoints
private function make_api_request($type_of_request = 'GET', $endpoint, $body){
$url = $this->base_url . $endpoint;
$header_token = "QB-USER-TOKEN " . $this->user_token;
$headers = array(
"QB-Realm-Hostname: $this->realm",
"User-Agent: QuickBaseRestApiApp",
"Authorization:". $header_token,
"Content-Type: application/json",
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
if($type_of_request == 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
}
$response = curl_exec($ch);
//This catches errors with the cURL request and logs them. Change the executable code to fit your error logging procedures
if(curl_errno($ch)){
error_log("There was an error with the QuickBaseRestApi call/n". "The HTTP Error Code recieved was: ".curl_errno($ch));
}
return $response;
}
/*--------------------------------------------
APP METHODS
---------------------------------------------*/
/**
* Get an app
*
* @see https://developer.quickbase.com/operation/getApp
*
* @param string $app_id Required.
*
* @return mixed $result
*/
public function get_an_app($app_id){
$endpoint = "/apps/$app_id";
$result = $this->make_api_request("GET", $endpoint);
return $result;
}
/*--------------------------------------------
TABLE METHODS
---------------------------------------------*/
/**
* Get a table from your app
*
* @see https://developer.quickbase.com/operation/getTable
*
* @param string $table_id Required.
* @param string $app_id Required.
*
* @return mixed $result
*/
public function get_a_table($table_id, $app_id){
$endpoint = "/tables/$table_id?appId=$app_id";
$result = $this->make_api_request("GET", $endpoint);
return $result;
}
/**
* Method to create a table
*
* @see https://developer.quickbase.com/operation/createTable
*
* @param string $app_id Required.
* @param string $add_table_data See below and documentation link above.Required.
* $update_data = array(
* "name": (string) Table name
* "description": (string) Table Description
* "singleRecordName": (string) Record name
* "pluralRecordName": (string) Plural Record Name
* );
*
* @return mixed $result
*/
public function create_a_table($app_id, $add_table_data){
$endpoint = "/tables?appId=$app_id";
$body = json_encode( $add_table_data );
$result = $this->make_api_request("POST", $endpoint, $body);
return $result;
}
/**
* Method to update a table
*
* @see https://developer.quickbase.com/operation/updateTable
*
* @param string $table_id Required.
* @param string $app_id Required.
* @param string $update_table_data See below and documentation link above.Required.
* $update_data = array(
* "name": (string) Table name
* "description": (string) Table Description
* "singleRecordName": (string) Record name
* "pluralRecordName": (string) Plural Record Name
* );
*
* @return mixed $result
*/
public function update_a_table($table_id, $app_id, $update_table_data){
$endpoint = "/tables/$table_id?appId=$app_id";
$body = json_encode( $update_table_data );
$result = $this->make_api_request("POST", $endpoint, $body);
return $result;
}
/*--------------------------------------------
REPORT METHODS
---------------------------------------------*/
/**
* Get all reports from a table
*
* @see https://developer.quickbase.com/operation/getTableReports
*
* @param string $table_id Required.
*
* @return mixed $result
*/
public function get_reports_for_a_table($table_id){
$endpoint = "/reports?tableId=$table_id";
$result = $this->make_api_request("GET", $endpoint);
return $result;
}
/**
* Get a single report
*
* @see https://developer.quickbase.com/operation/getReport
*
* @param string $report_id Required.
* @param string $table_id Required.
*
* @return mixed $result
*/
public function get_single_report( $report_id ,$table_id ){
$endpoint = "/reports/$report_id?tableId=$table_id";
$result = $this->make_api_request("GET", $endpoint);
return $result;
}
/*--------------------------------------------
RECORD METHODS
---------------------------------------------*/
/**
* Make a query for record data
*
* @see https://developer.quickbase.com/operation/runQuery
*
* @param string $table_id The table to query. Required
* @param array $select Array of field ids. Required
* @param string $where A Quickbase query language formatted bracket enclosed string see documentation link above. Required
* $where = {3.CT.'string'}
* @param array $sort_by A multidimensional array correctly formatted see below. See documentation link above. Optional
* $sort_by = array(
* array(
* "fieldId" => (int|string) The field id to sort by.
* "order" => "ASC|DESC" (string) which order parameter.
* ) ...add as many sorting parameters as allowed
* )
*
* @param array $group_by A multidimensional array correctly formatted. See documentation link above. Optional
* $group_by = array(
* array(
* "fieldId" => (int|string) The field id to group. Required
* "grouping" => "ASC|DESC|equal values" (string) which grouping. Required
* )
* )
*
* @param array $options An array of options. See documentation link above. Optional
* $options = array(
* "skip" => (int) Number of records to skip. Optional
* "compareWithAppLocalTime", => (bool) See documentation. Optional
* "top" => (bool) Number of records to display. Optional
* )
*
* @return mixed $result
*/
public function query_for_data($table_id, $select, $where, $sort_by = '', $group_by = '', $options = ''){
$endpoint = "/records/query";
$select = json_encode( $select );
$where = ($where) ? $where: '';
$sort_by = ($sort_by) ? json_encode( $sort_by ): "[{}]";
$group_by = ($group_by) ? json_encode( $group_by ): "[{}]";
$options = ($options) ? ',"options":'.json_encode( $options ): "";
$body = "{ \"from\": \"$table_id\",\"select\": $select,\"where\" : \"$where\",\"sortBy\": $sort_by,\"groupBy\": $group_by $options }";
$result = $this->make_api_request("POST", $endpoint, $body);
return $result;
}
/**
* Update or create record(s)
*
* @see https://developer.quickbase.com/operation/upsert
*
* @param string $table_id
* @param array $values_to_update a multidimensional array see below
* $values_to_update = array(
* array(
* (string) table primary key field id in quotes. Required =>
* array("value" => (int|string) primary key id to update or new id. Required),
* (string) field id value in quotes =>
* array("value" => (int|string) value for field),
* (string) field id value in quotes =>
* array("value" => (int|string) value for field),
* ... put as may key value pairs that you need
* ),
* array(
* (string) another table primary key field id in quotes =>
* array("value" => (int|string) another record primary key id),
* (string) field id value in quotes =>
* array("value" => (int|string) value for field),
* ),
* ... put as many records as you need
* );
* @param array $fields_to_return A list of field ids to return after update. Optional
*
* @return mixed $result
*/
public function update_or_create_records($table_id, $values_to_update, $fields_to_return = array(3)){
$endpoint = "/records";
$data = json_encode( $values_to_update );
$fields_to_return = json_encode( $fields_to_return );
$body = "{
\"to\": \"$table_id\",
\"data\": $data,
\"fieldsToReturn\": $fields_to_return
}";
$result = $this->make_api_request("POST", $endpoint, $body);
return $result;
}
}
?>