-
Notifications
You must be signed in to change notification settings - Fork 0
/
airtab2php.php
167 lines (149 loc) · 5.35 KB
/
airtab2php.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
<?php
// #############################################
// FUNCTION: LIST
// - This function is based on "List Records"
// for Airtables API
// _____________________________________________
function airtab2php_list($apikey, $base, $table, $view, $fields, $where, $sort, $limit) {
if (!empty($apikey) && !empty($base) && !empty($table)) {
$param = "";
if (!empty($limit) && is_numeric($limit)) {
$param .= "&maxRecords=".$limit;
}
if (!empty($fields)) {
$fields = explode(";", $fields);
foreach ($fields as $field) {
$param .= "&".rawurlencode("fields[]")."=".$field;
}
}
if (!empty($where)) {
preg_match("/(=|!=)/", $where, $operator);
$where = explode($operator[0], $where);
$param .= "&filterByFormula=".rawurlencode("{".$where[0]."} ".$operator[0]." \"".$where[1]."\"");
}
if (!empty($sort)) {
$sort = explode(";", $sort);
$param .= "&".rawurlencode("sort[0][field]")."=".$sort[0]."&".rawurlencode("sort[0][direction]")."=".$sort[1];
}
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, "https://api.airtable.com/v0/".$base."/".$table."?view=".rawurlencode($view).$param);
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer '.$apikey
);
curl_setopt($cURL, CURLOPT_HTTPHEADER, $headers);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_HTTPGET, true);
$response = curl_exec($cURL);
$httpCode = curl_getinfo($cURL, CURLINFO_HTTP_CODE);
$result = json_decode($response, true);
curl_close($cURL);
if ($httpCode == 200 && $result['records']) {
return $result['records'];
} else {
return FALSE;
}
} else {
return FALSE;
}
}
// #############################################
// FUNCTION: CREATE
// - This function is based on "Create Records"
// for Airtables API
// _____________________________________________
function airtab2php_create($apikey, $base, $table, $fields, $values) {
if (!empty($apikey) && !empty($base) && !empty($table) && !empty($fields) && !empty($values)) {
$fields = explode(";", $fields);
$values = explode(";", $values);
$data = array_combine($fields, $values);
$data = json_encode(array('fields' => $data), true);
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, "https://api.airtable.com/v0/".$base."/".$table);
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer '.$apikey
);
curl_setopt($cURL, CURLOPT_HTTPHEADER, $headers);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cURL, CURLOPT_POST, true);
curl_setopt($cURL, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($cURL);
$httpCode = curl_getinfo($cURL, CURLINFO_HTTP_CODE);
$result = json_decode($response, true);
curl_close($cURL);
if ($httpCode == 200 && !empty($result['id'])) {
return TRUE;
} else {
return FALSE;
}
} else {
return FALSE;
}
}
// #############################################
// FUNCTION: UPDATE
// - This function is based on "Update Records"
// for Airtables API
// _____________________________________________
function airtab2php_update($apikey, $base, $table, $id, $fields, $values) {
if (!empty($apikey) && !empty($base) && !empty($table) && !empty($id) && !empty($fields) && !empty($values)) {
$fields = explode(";", $fields);
$values = explode(";", $values);
$data = array_combine($fields, $values);
$data = json_encode(array('fields' => $data), true);
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, "https://api.airtable.com/v0/".$base."/".$table."/".$id);
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer '.$apikey
);
curl_setopt($cURL, CURLOPT_HTTPHEADER, $headers);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cURL, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($cURL, CURLOPT_POST, true);
curl_setopt($cURL, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($cURL);
$httpCode = curl_getinfo($cURL, CURLINFO_HTTP_CODE);
$result = json_decode($response, true);
curl_close($cURL);
if ($httpCode == 200 && !empty($result['id'])) {
return TRUE;
} else {
return FALSE;
}
} else {
return FALSE;
}
}
// #############################################
// FUNCTION: DELETE
// - This function is based on "Delete Records"
// for Airtables API
// _____________________________________________
function airtab2php_delete($apikey, $base, $table, $id) {
if (!empty($apikey) && !empty($base) && !empty($table) && !empty($id)) {
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, "https://api.airtable.com/v0/".$base."/".$table."/".$id);
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer '.$apikey
);
curl_setopt($cURL, CURLOPT_HTTPHEADER, $headers);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cURL, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($cURL, CURLOPT_POST, true);
$response = curl_exec($cURL);
$httpCode = curl_getinfo($cURL, CURLINFO_HTTP_CODE);
$result = json_decode($response, true);
curl_close($cURL);
if ($httpCode == 200 && $result['deleted']) {
return TRUE;
} else {
return FALSE;
}
} else {
return FALSE;
}
}
?>