103 |
- |
1 |
<?php
|
|
|
2 |
// If you are using Composer
|
|
|
3 |
require 'vendor/autoload.php';
|
|
|
4 |
|
|
|
5 |
|
|
|
6 |
$apiKey = getenv('SENDGRID_API_KEY');
|
|
|
7 |
$sg = new \SendGrid($apiKey);
|
|
|
8 |
|
|
|
9 |
////////////////////////////////////////////////////
|
|
|
10 |
// Create a Custom Field #
|
|
|
11 |
// POST /contactdb/custom_fields #
|
|
|
12 |
|
|
|
13 |
$request_body = json_decode('{
|
|
|
14 |
"name": "pet",
|
|
|
15 |
"type": "text"
|
|
|
16 |
}');
|
|
|
17 |
$response = $sg->client->contactdb()->custom_fields()->post($request_body);
|
|
|
18 |
echo $response->statusCode();
|
|
|
19 |
echo $response->body();
|
|
|
20 |
echo $response->headers();
|
|
|
21 |
|
|
|
22 |
////////////////////////////////////////////////////
|
|
|
23 |
// Retrieve all custom fields #
|
|
|
24 |
// GET /contactdb/custom_fields #
|
|
|
25 |
|
|
|
26 |
$response = $sg->client->contactdb()->custom_fields()->get();
|
|
|
27 |
echo $response->statusCode();
|
|
|
28 |
echo $response->body();
|
|
|
29 |
echo $response->headers();
|
|
|
30 |
|
|
|
31 |
////////////////////////////////////////////////////
|
|
|
32 |
// Retrieve a Custom Field #
|
|
|
33 |
// GET /contactdb/custom_fields/{custom_field_id} #
|
|
|
34 |
|
|
|
35 |
$custom_field_id = "test_url_param";
|
|
|
36 |
$response = $sg->client->contactdb()->custom_fields()->_($custom_field_id)->get();
|
|
|
37 |
echo $response->statusCode();
|
|
|
38 |
echo $response->body();
|
|
|
39 |
echo $response->headers();
|
|
|
40 |
|
|
|
41 |
////////////////////////////////////////////////////
|
|
|
42 |
// Delete a Custom Field #
|
|
|
43 |
// DELETE /contactdb/custom_fields/{custom_field_id} #
|
|
|
44 |
|
|
|
45 |
$custom_field_id = "test_url_param";
|
|
|
46 |
$response = $sg->client->contactdb()->custom_fields()->_($custom_field_id)->delete();
|
|
|
47 |
echo $response->statusCode();
|
|
|
48 |
echo $response->body();
|
|
|
49 |
echo $response->headers();
|
|
|
50 |
|
|
|
51 |
////////////////////////////////////////////////////
|
|
|
52 |
// Create a List #
|
|
|
53 |
// POST /contactdb/lists #
|
|
|
54 |
|
|
|
55 |
$request_body = json_decode('{
|
|
|
56 |
"name": "your list name"
|
|
|
57 |
}');
|
|
|
58 |
$response = $sg->client->contactdb()->lists()->post($request_body);
|
|
|
59 |
echo $response->statusCode();
|
|
|
60 |
echo $response->body();
|
|
|
61 |
echo $response->headers();
|
|
|
62 |
|
|
|
63 |
////////////////////////////////////////////////////
|
|
|
64 |
// Retrieve all lists #
|
|
|
65 |
// GET /contactdb/lists #
|
|
|
66 |
|
|
|
67 |
$response = $sg->client->contactdb()->lists()->get();
|
|
|
68 |
echo $response->statusCode();
|
|
|
69 |
echo $response->body();
|
|
|
70 |
echo $response->headers();
|
|
|
71 |
|
|
|
72 |
////////////////////////////////////////////////////
|
|
|
73 |
// Delete Multiple lists #
|
|
|
74 |
// DELETE /contactdb/lists #
|
|
|
75 |
|
|
|
76 |
$request_body = json_decode('[
|
|
|
77 |
1,
|
|
|
78 |
2,
|
|
|
79 |
3,
|
|
|
80 |
4
|
|
|
81 |
]');
|
|
|
82 |
$response = $sg->client->contactdb()->lists()->delete($request_body);
|
|
|
83 |
echo $response->statusCode();
|
|
|
84 |
echo $response->body();
|
|
|
85 |
echo $response->headers();
|
|
|
86 |
|
|
|
87 |
////////////////////////////////////////////////////
|
|
|
88 |
// Update a List #
|
|
|
89 |
// PATCH /contactdb/lists/{list_id} #
|
|
|
90 |
|
|
|
91 |
$request_body = json_decode('{
|
|
|
92 |
"name": "newlistname"
|
|
|
93 |
}');
|
|
|
94 |
$query_params = json_decode('{"list_id": 1}');
|
|
|
95 |
$list_id = "test_url_param";
|
|
|
96 |
$response = $sg->client->contactdb()->lists()->_($list_id)->patch($request_body, $query_params);
|
|
|
97 |
echo $response->statusCode();
|
|
|
98 |
echo $response->body();
|
|
|
99 |
echo $response->headers();
|
|
|
100 |
|
|
|
101 |
////////////////////////////////////////////////////
|
|
|
102 |
// Retrieve a single list #
|
|
|
103 |
// GET /contactdb/lists/{list_id} #
|
|
|
104 |
|
|
|
105 |
$query_params = json_decode('{"list_id": 1}');
|
|
|
106 |
$list_id = "test_url_param";
|
|
|
107 |
$response = $sg->client->contactdb()->lists()->_($list_id)->get(null, $query_params);
|
|
|
108 |
echo $response->statusCode();
|
|
|
109 |
echo $response->body();
|
|
|
110 |
echo $response->headers();
|
|
|
111 |
|
|
|
112 |
////////////////////////////////////////////////////
|
|
|
113 |
// Delete a List #
|
|
|
114 |
// DELETE /contactdb/lists/{list_id} #
|
|
|
115 |
|
|
|
116 |
$query_params = json_decode('{"delete_contacts": "true"}');
|
|
|
117 |
$list_id = "test_url_param";
|
|
|
118 |
$response = $sg->client->contactdb()->lists()->_($list_id)->delete(null, $query_params);
|
|
|
119 |
echo $response->statusCode();
|
|
|
120 |
echo $response->body();
|
|
|
121 |
echo $response->headers();
|
|
|
122 |
|
|
|
123 |
////////////////////////////////////////////////////
|
|
|
124 |
// Add Multiple Recipients to a List #
|
|
|
125 |
// POST /contactdb/lists/{list_id}/recipients #
|
|
|
126 |
|
|
|
127 |
$request_body = json_decode('[
|
|
|
128 |
"recipient_id1",
|
|
|
129 |
"recipient_id2"
|
|
|
130 |
]');
|
|
|
131 |
$list_id = "test_url_param";
|
|
|
132 |
$response = $sg->client->contactdb()->lists()->_($list_id)->recipients()->post($request_body);
|
|
|
133 |
echo $response->statusCode();
|
|
|
134 |
echo $response->body();
|
|
|
135 |
echo $response->headers();
|
|
|
136 |
|
|
|
137 |
////////////////////////////////////////////////////
|
|
|
138 |
// Retrieve all recipients on a List #
|
|
|
139 |
// GET /contactdb/lists/{list_id}/recipients #
|
|
|
140 |
|
|
|
141 |
$query_params = json_decode('{"page": 1, "page_size": 1, "list_id": 1}');
|
|
|
142 |
$list_id = "test_url_param";
|
|
|
143 |
$response = $sg->client->contactdb()->lists()->_($list_id)->recipients()->get(null, $query_params);
|
|
|
144 |
echo $response->statusCode();
|
|
|
145 |
echo $response->body();
|
|
|
146 |
echo $response->headers();
|
|
|
147 |
|
|
|
148 |
////////////////////////////////////////////////////
|
|
|
149 |
// Add a Single Recipient to a List #
|
|
|
150 |
// POST /contactdb/lists/{list_id}/recipients/{recipient_id} #
|
|
|
151 |
|
|
|
152 |
$list_id = "test_url_param";
|
|
|
153 |
$recipient_id = "test_url_param";
|
|
|
154 |
$response = $sg->client->contactdb()->lists()->_($list_id)->recipients()->_($recipient_id)->post();
|
|
|
155 |
echo $response->statusCode();
|
|
|
156 |
echo $response->body();
|
|
|
157 |
echo $response->headers();
|
|
|
158 |
|
|
|
159 |
////////////////////////////////////////////////////
|
|
|
160 |
// Delete a Single Recipient from a Single List #
|
|
|
161 |
// DELETE /contactdb/lists/{list_id}/recipients/{recipient_id} #
|
|
|
162 |
|
|
|
163 |
$query_params = json_decode('{"recipient_id": 1, "list_id": 1}');
|
|
|
164 |
$list_id = "test_url_param";
|
|
|
165 |
$recipient_id = "test_url_param";
|
|
|
166 |
$response = $sg->client->contactdb()->lists()->_($list_id)->recipients()->_($recipient_id)->delete(null, $query_params);
|
|
|
167 |
echo $response->statusCode();
|
|
|
168 |
echo $response->body();
|
|
|
169 |
echo $response->headers();
|
|
|
170 |
|
|
|
171 |
////////////////////////////////////////////////////
|
|
|
172 |
// Update Recipient #
|
|
|
173 |
// PATCH /contactdb/recipients #
|
|
|
174 |
|
|
|
175 |
$request_body = json_decode('[
|
|
|
176 |
{
|
|
|
177 |
"email": "jones@example.com",
|
|
|
178 |
"first_name": "Guy",
|
|
|
179 |
"last_name": "Jones"
|
|
|
180 |
}
|
|
|
181 |
]');
|
|
|
182 |
$response = $sg->client->contactdb()->recipients()->patch($request_body);
|
|
|
183 |
echo $response->statusCode();
|
|
|
184 |
echo $response->body();
|
|
|
185 |
echo $response->headers();
|
|
|
186 |
|
|
|
187 |
////////////////////////////////////////////////////
|
|
|
188 |
// Add recipients #
|
|
|
189 |
// POST /contactdb/recipients #
|
|
|
190 |
|
|
|
191 |
$request_body = json_decode('[
|
|
|
192 |
{
|
|
|
193 |
"age": 25,
|
|
|
194 |
"email": "example@example.com",
|
|
|
195 |
"first_name": "",
|
|
|
196 |
"last_name": "User"
|
|
|
197 |
},
|
|
|
198 |
{
|
|
|
199 |
"age": 25,
|
|
|
200 |
"email": "example2@example.com",
|
|
|
201 |
"first_name": "Example",
|
|
|
202 |
"last_name": "User"
|
|
|
203 |
}
|
|
|
204 |
]');
|
|
|
205 |
$response = $sg->client->contactdb()->recipients()->post($request_body);
|
|
|
206 |
echo $response->statusCode();
|
|
|
207 |
echo $response->body();
|
|
|
208 |
echo $response->headers();
|
|
|
209 |
|
|
|
210 |
////////////////////////////////////////////////////
|
|
|
211 |
// Retrieve recipients #
|
|
|
212 |
// GET /contactdb/recipients #
|
|
|
213 |
|
|
|
214 |
$query_params = json_decode('{"page": 1, "page_size": 1}');
|
|
|
215 |
$response = $sg->client->contactdb()->recipients()->get(null, $query_params);
|
|
|
216 |
echo $response->statusCode();
|
|
|
217 |
echo $response->body();
|
|
|
218 |
echo $response->headers();
|
|
|
219 |
|
|
|
220 |
////////////////////////////////////////////////////
|
|
|
221 |
// Delete Recipient #
|
|
|
222 |
// DELETE /contactdb/recipients #
|
|
|
223 |
|
|
|
224 |
$request_body = json_decode('[
|
|
|
225 |
"recipient_id1",
|
|
|
226 |
"recipient_id2"
|
|
|
227 |
]');
|
|
|
228 |
$response = $sg->client->contactdb()->recipients()->delete($request_body);
|
|
|
229 |
echo $response->statusCode();
|
|
|
230 |
echo $response->body();
|
|
|
231 |
echo $response->headers();
|
|
|
232 |
|
|
|
233 |
////////////////////////////////////////////////////
|
|
|
234 |
// Retrieve the count of billable recipients #
|
|
|
235 |
// GET /contactdb/recipients/billable_count #
|
|
|
236 |
|
|
|
237 |
$response = $sg->client->contactdb()->recipients()->billable_count()->get();
|
|
|
238 |
echo $response->statusCode();
|
|
|
239 |
echo $response->body();
|
|
|
240 |
echo $response->headers();
|
|
|
241 |
|
|
|
242 |
////////////////////////////////////////////////////
|
|
|
243 |
// Retrieve a Count of Recipients #
|
|
|
244 |
// GET /contactdb/recipients/count #
|
|
|
245 |
|
|
|
246 |
$response = $sg->client->contactdb()->recipients()->count()->get();
|
|
|
247 |
echo $response->statusCode();
|
|
|
248 |
echo $response->body();
|
|
|
249 |
echo $response->headers();
|
|
|
250 |
|
|
|
251 |
////////////////////////////////////////////////////
|
|
|
252 |
// Retrieve recipients matching search criteria #
|
|
|
253 |
// GET /contactdb/recipients/search #
|
|
|
254 |
|
|
|
255 |
$query_params = json_decode('{"{field_name}": "test_string"}');
|
|
|
256 |
$response = $sg->client->contactdb()->recipients()->search()->get(null, $query_params);
|
|
|
257 |
echo $response->statusCode();
|
|
|
258 |
echo $response->body();
|
|
|
259 |
echo $response->headers();
|
|
|
260 |
|
|
|
261 |
////////////////////////////////////////////////////
|
|
|
262 |
// Retrieve a single recipient #
|
|
|
263 |
// GET /contactdb/recipients/{recipient_id} #
|
|
|
264 |
|
|
|
265 |
$recipient_id = "test_url_param";
|
|
|
266 |
$response = $sg->client->contactdb()->recipients()->_($recipient_id)->get();
|
|
|
267 |
echo $response->statusCode();
|
|
|
268 |
echo $response->body();
|
|
|
269 |
echo $response->headers();
|
|
|
270 |
|
|
|
271 |
////////////////////////////////////////////////////
|
|
|
272 |
// Delete a Recipient #
|
|
|
273 |
// DELETE /contactdb/recipients/{recipient_id} #
|
|
|
274 |
|
|
|
275 |
$recipient_id = "test_url_param";
|
|
|
276 |
$response = $sg->client->contactdb()->recipients()->_($recipient_id)->delete();
|
|
|
277 |
echo $response->statusCode();
|
|
|
278 |
echo $response->body();
|
|
|
279 |
echo $response->headers();
|
|
|
280 |
|
|
|
281 |
////////////////////////////////////////////////////
|
|
|
282 |
// Retrieve the lists that a recipient is on #
|
|
|
283 |
// GET /contactdb/recipients/{recipient_id}/lists #
|
|
|
284 |
|
|
|
285 |
$recipient_id = "test_url_param";
|
|
|
286 |
$response = $sg->client->contactdb()->recipients()->_($recipient_id)->lists()->get();
|
|
|
287 |
echo $response->statusCode();
|
|
|
288 |
echo $response->body();
|
|
|
289 |
echo $response->headers();
|
|
|
290 |
|
|
|
291 |
////////////////////////////////////////////////////
|
|
|
292 |
// Retrieve reserved fields #
|
|
|
293 |
// GET /contactdb/reserved_fields #
|
|
|
294 |
|
|
|
295 |
$response = $sg->client->contactdb()->reserved_fields()->get();
|
|
|
296 |
echo $response->statusCode();
|
|
|
297 |
echo $response->body();
|
|
|
298 |
echo $response->headers();
|
|
|
299 |
|
|
|
300 |
////////////////////////////////////////////////////
|
|
|
301 |
// Create a Segment #
|
|
|
302 |
// POST /contactdb/segments #
|
|
|
303 |
|
|
|
304 |
$request_body = json_decode('{
|
|
|
305 |
"conditions": [
|
|
|
306 |
{
|
|
|
307 |
"and_or": "",
|
|
|
308 |
"field": "last_name",
|
|
|
309 |
"operator": "eq",
|
|
|
310 |
"value": "Miller"
|
|
|
311 |
},
|
|
|
312 |
{
|
|
|
313 |
"and_or": "and",
|
|
|
314 |
"field": "last_clicked",
|
|
|
315 |
"operator": "gt",
|
|
|
316 |
"value": "01/02/2015"
|
|
|
317 |
},
|
|
|
318 |
{
|
|
|
319 |
"and_or": "or",
|
|
|
320 |
"field": "clicks.campaign_identifier",
|
|
|
321 |
"operator": "eq",
|
|
|
322 |
"value": "513"
|
|
|
323 |
}
|
|
|
324 |
],
|
|
|
325 |
"list_id": 4,
|
|
|
326 |
"name": "Last Name Miller"
|
|
|
327 |
}');
|
|
|
328 |
$response = $sg->client->contactdb()->segments()->post($request_body);
|
|
|
329 |
echo $response->statusCode();
|
|
|
330 |
echo $response->body();
|
|
|
331 |
echo $response->headers();
|
|
|
332 |
|
|
|
333 |
////////////////////////////////////////////////////
|
|
|
334 |
// Retrieve all segments #
|
|
|
335 |
// GET /contactdb/segments #
|
|
|
336 |
|
|
|
337 |
$response = $sg->client->contactdb()->segments()->get();
|
|
|
338 |
echo $response->statusCode();
|
|
|
339 |
echo $response->body();
|
|
|
340 |
echo $response->headers();
|
|
|
341 |
|
|
|
342 |
////////////////////////////////////////////////////
|
|
|
343 |
// Update a segment #
|
|
|
344 |
// PATCH /contactdb/segments/{segment_id} #
|
|
|
345 |
|
|
|
346 |
$request_body = json_decode('{
|
|
|
347 |
"conditions": [
|
|
|
348 |
{
|
|
|
349 |
"and_or": "",
|
|
|
350 |
"field": "last_name",
|
|
|
351 |
"operator": "eq",
|
|
|
352 |
"value": "Miller"
|
|
|
353 |
}
|
|
|
354 |
],
|
|
|
355 |
"list_id": 5,
|
|
|
356 |
"name": "The Millers"
|
|
|
357 |
}');
|
|
|
358 |
$query_params = json_decode('{"segment_id": "test_string"}');
|
|
|
359 |
$segment_id = "test_url_param";
|
|
|
360 |
$response = $sg->client->contactdb()->segments()->_($segment_id)->patch($request_body, $query_params);
|
|
|
361 |
echo $response->statusCode();
|
|
|
362 |
echo $response->body();
|
|
|
363 |
echo $response->headers();
|
|
|
364 |
|
|
|
365 |
////////////////////////////////////////////////////
|
|
|
366 |
// Retrieve a segment #
|
|
|
367 |
// GET /contactdb/segments/{segment_id} #
|
|
|
368 |
|
|
|
369 |
$query_params = json_decode('{"segment_id": 1}');
|
|
|
370 |
$segment_id = "test_url_param";
|
|
|
371 |
$response = $sg->client->contactdb()->segments()->_($segment_id)->get(null, $query_params);
|
|
|
372 |
echo $response->statusCode();
|
|
|
373 |
echo $response->body();
|
|
|
374 |
echo $response->headers();
|
|
|
375 |
|
|
|
376 |
////////////////////////////////////////////////////
|
|
|
377 |
// Delete a segment #
|
|
|
378 |
// DELETE /contactdb/segments/{segment_id} #
|
|
|
379 |
|
|
|
380 |
$query_params = json_decode('{"delete_contacts": "true"}');
|
|
|
381 |
$segment_id = "test_url_param";
|
|
|
382 |
$response = $sg->client->contactdb()->segments()->_($segment_id)->delete(null, $query_params);
|
|
|
383 |
echo $response->statusCode();
|
|
|
384 |
echo $response->body();
|
|
|
385 |
echo $response->headers();
|
|
|
386 |
|
|
|
387 |
////////////////////////////////////////////////////
|
|
|
388 |
// Retrieve recipients on a segment #
|
|
|
389 |
// GET /contactdb/segments/{segment_id}/recipients #
|
|
|
390 |
|
|
|
391 |
$query_params = json_decode('{"page": 1, "page_size": 1}');
|
|
|
392 |
$segment_id = "test_url_param";
|
|
|
393 |
$response = $sg->client->contactdb()->segments()->_($segment_id)->recipients()->get(null, $query_params);
|
|
|
394 |
echo $response->statusCode();
|
|
|
395 |
echo $response->body();
|
|
|
396 |
echo $response->headers();
|