-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sendinblue.php
executable file
·167 lines (151 loc) · 6.31 KB
/
Sendinblue.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
require_once(__DIR__ . '/vendor/autoload.php');
class SendinBlueManager {
/**
* @var
*/
protected $config;
/**
* @var string $api_key
*/
protected $api_key = '';
/**
* @var bool $debug
*/
public $debug = false;
/**
* @param string $key
*/
public function __construct(string $key, bool $debug = false) {
$this->api_key = $key;
$this->debug = $debug;
$this->init();
}
/**
*
*/
private function init() {
if(!empty($this->api_key)) {
// Configure API key authorization: api-key
$this->config = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKey('api-key', $this->api_key);
$apiInstance = new SendinBlue\Client\Api\AccountApi(
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
// This is optional, `GuzzleHttp\Client` will be used as default.
new GuzzleHttp\Client(),
$this->config
);
if($this->debug) {
try {
$result = $apiInstance ->getAccount();
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling AccountApi->getAccount: ', $e->getMessage(), PHP_EOL;
}
}
}
}
/**
* @param int $limit | Number of documents per page
* @param int $offset | Index of the first document of the page
* @param string $sort | Sort the results in the ascending/descending order of record creation. Default order is **descending** if `sort` is not passed
* @return array|\SendinBlue\Client\Model\GetLists
* @throws \SendinBlue\Client\ApiException
*/
public function getLists(int $limit = 10, int $offset = 0, $sort = "desc") {
$lists = [];
$apiInstance = new SendinBlue\Client\Api\ContactsApi(
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
// This is optional, `GuzzleHttp\Client` will be used as default.
new GuzzleHttp\Client(),
$this->config
);
if($this->debug) {
try {
$lists = $apiInstance ->getLists($limit, $offset, $sort);
print_r($lists);
} catch (Exception $e) {
echo 'Exception when calling ContactsApi->getLists: ', $e->getMessage(), PHP_EOL;
}
}
else {
$lists = $apiInstance ->getLists($limit, $offset, $sort);
}
return $lists;
}
/**
* @param int $listId | Id of the list
* @param int $limit | Number of documents per page
* @param int $offset | Index of the first document of the page
* @param string $sort | Sort the results in the ascending/descending order of record creation. Default order is **descending** if `sort` is not passed
* @param string $modifiedSince | Filter (urlencoded) the contacts modified after a given UTC date-time (YYYY-MM-DDTHH:mm:ss.SSSZ). Prefer to pass your timezone in date-time format for accurate result.
*/
public function getMembers(int $listId, int $limit = 25, int $offset = 0, string $sort = "desc", string $modifiedSince = '') {
$membersList = [
'total_items' => 0,
'members' => [],
];
$apiInstance = new SendinBlue\Client\Api\ContactsApi(
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
// This is optional, `GuzzleHttp\Client` will be used as default.
new GuzzleHttp\Client(),
$this->config
);
if($this->debug) {
try {
$membersList = $apiInstance->getContactsFromList($listId, $modifiedSince, $limit, $offset, $sort);
print_r($membersList);
} catch (Exception $e) {
echo 'Exception when calling ContactsApi->getContactsFromList: ', $e->getMessage(), PHP_EOL;
}
}
else {
$membersList = $apiInstance->getContactsFromList($listId, $modifiedSince, $limit, $offset, $sort);
}
return $membersList;
}
/**
* @param int $listId
* @param string $email
* @param string $firstname
* @param string $lastname
*/
public function addMemberToList(int $listId, string $email, string $firstname = '', string $lastname = '') {
$apiInstance = new SendinBlue\Client\Api\ContactsApi(
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
// This is optional, `GuzzleHttp\Client` will be used as default.
new GuzzleHttp\Client(),
$this->config
);
$createContact = new SendinBlue\Client\Model\CreateContact(); // Values to create a contact
$createContact["updateEnabled"] = true;
$createContact['email'] = $email;
$createContact['listIds'] = [$listId];
//if(!empty($firstname)) $createContact['attributes']["PRENOM"] = $firstname;
//if(!empty($lastname)) $createContact['attributes']["NOM"] = $lastname;
if($this->debug) {
try {
$result = $apiInstance->createContact($createContact);
print_r($result);
} catch (Exception $e) {
print_r($e);
echo 'Exception when calling ContactsApi->createContact: ', $e->getMessage(), PHP_EOL;
}
}
else {
$apiInstance->createContact($createContact);
}
$identifier = $email;
$updateContact = new \SendinBlue\Client\Model\UpdateContact();
$updateContact['attributes'] = ['PRENOM'=>$firstname, 'NOM'=>$lastname];
if($this->debug) {
try {
$apiInstance->updateContact($identifier, $updateContact);
} catch (Exception $e) {
echo 'Exception when calling ContactsApi->updateContact: ', $e->getMessage(), PHP_EOL;
}
}
else {
$apiInstance->updateContact($identifier, $updateContact);
}
}
}