-
Notifications
You must be signed in to change notification settings - Fork 0
/
Flodesk.php
99 lines (78 loc) · 2.66 KB
/
Flodesk.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
<?php
class Flodesk {
protected $baseUrl;
protected $apikey;
protected $data = [];
protected $method = "POST";
protected $userAgent = "";
public function __construct($apikey, $baseUrl = "https://api.flodesk.com/v1", $userAgent = "ThrivingSmart (www.thrivingsmart.com)") {
$this->apikey = $apikey;
$this->baseUrl = $baseUrl;
$this->userAgent = $userAgent;
}
public function create($email, $fname = '', $lname = '') {
$endpoint = $this->baseUrl . "/subscribers";
$this->data = [
'email' => $email,
'first_name' => $fname,
'last_name' => $lname
];
return $this->post($endpoint);
}
public function subscribe($segmentId, $email) {
$endpoint = $this->baseUrl . "/subscribers/$email/segments";
$this->data = ["segment_ids" => [$segmentId, ],];
return $this->post($endpoint);
}
public function unsubscribe($segmentId, $email) {
$endpoint = $this->baseUrl . "/subscribers/{$email}/segments";
$this->data = ["segment_ids" => [$segmentId, ],];
return $this->delete($endpoint);
}
public function list() {
$endpoint = $this->baseUrl . "/subscribers";
$this->data = [];
return $this->get($endpoint);
}
protected function send($endpoint) {
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->method);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
"User-Agent: " . $this->userAgent,
"Authorization: Basic " . base64_encode($this->apikey),
]);
if (! empty($this->data)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->data));
}
$response = curl_exec($ch);
if (!$response) {
$error = curl_errno($ch);
curl_close($ch);
// in a live project, you should throw exception and handle the exceptions in your code
var_dump($error);
die();
}
curl_close($ch);
return $response;
}
protected function post($url) {
$this->method = "POST";
$reponse = $this->send($url);
$this->data = [];
return $reponse;
}
protected function get($url) {
$this->method = "GET";
$reponse = $this->send($url);
$this->data = [];
return $reponse;
}
protected function delete($url) {
$this->method = "DELETE";
$reponse = $this->send($url);
$this->data = [];
return $reponse;
}
}