forked from Russlanpopular/php-api-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnisenderApi.php
464 lines (416 loc) · 15.4 KB
/
UnisenderApi.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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
<?php
namespace Unisender\ApiWrapper;
/**
* API UniSender.
*
* @link https://www.unisender.com/en/support/integration/api/
* @link https://www.unisender.com/ru/support/integration/api/
*
* @method sendSms(array $params) It is a method for easy sending the one SMS to one or several recipients.
* @method sendEmail(array $params) It is a method to send a single individual email without personalization and
* with limited possibilities to obtain statistics. To send transactional letters, use the
* UniOne — the transactional letter service from UniSender. https://www.unisender.com/en/features/unione/
* @method getLists() It is a method to get the list of all available campaign lists.
* @method createList(array $params) It is a method to create a new contact list.
* @method updateList(array $params) It is a method to change campaign list properties.
* @method deleteList(array $params) It is a method to delete a list.
* @method exclude(array $params) The method excludes the contact’s email or phone number from one or several lists.
* @method unsubscribe(array $params) The method unsubscribes the contact email or phone number from one or several
* lists.
* @method importContacts(array $params) It is a method of bulk import of contacts.
* @method getTotalContactsCount(array $params) The method returns the contacts database size by the user login.
* @method getContactCount(array $params) Get contact count in list.
* @method createEmailMessage(array $params) It is a method to create an email without sending it.
* @method createSmsMessage(array $params) It is a method to create SMS messages without sending them.
* @method createCampaign(array $params) This method is used to schedule or immediately start sending email
* or SMS messages.
* @method getActualMessageVersion(array $params) The method returns the id of the relevant version of
* the specified letter.
* @method checkSms(array $params) It returns a string — the SMS sending status.
* @method sendTestEmail(array $params) It is a method to send a test email message.
* @method checkEmail(array $params) The method allows you to check the delivery status of emails sent
* using the sendEmail method.
* @method updateOptInEmail(array $params) Each campaign list has the attached text of the invitation
* to subscribe and confirm the email that is sent to the contact to confirm the campaign. The text of the letter
* can be changed using the updateOptInEmail method.
* @method getWebVersion(array $params) It is a method to get the link to the web version of the letter.
* @method deleteMessage(array $params) It is a method to delete a message.
* @method createEmailTemplate(array $params) It is a method to create an email template for a mass campaign.
* @method updateEmailTemplate(array $params) It is a method to edit email templates for a mass campaign.
* @method deleteTemplate(array $params) It is a method to delete a template.
* @method getTemplate(array $params) The method returns information about the specified template.
* @method getTemplates(array $params = []) This method is used to get the list of templates created
* both through the UniSender personal account and through the API.
* @method listTemplates(array $params = []) This method is used to get the list of templates created both
* through the UniSender personal account and through the API.
* @method getCampaignCommonStats(array $params) The method returns statistics similar to «Campaigns».
* @method getVisitedLinks(array $params) Get a report on the links visited by users in the specified email campaign.
* @method getCampaigns(array $params = array()) It is a method to get the list of all available campaigns.
* @method getCampaignStatus(array $params) Find out the status of the campaign created using the createCampaign method.
* @method getMessages(array $params = []) This method is used to get the list of letters created both
* through the UniSender personal account and through the API.
* @method getMessage(array $params) It is a method to get information about SMS or email message.
* @method listMessages(array $params) This method is used to get the list of messages created both through
* the UniSender personal account and through the API. The method works like getMessages, the difference of
* listMessages is that the letter body and attachments are not returned, while the user login is returned. To get the
* body and attachments, use the getMessage method.
* @method getFields() It is a method to get the list of user fields.
* @method createField(array $params) It is a method to create a new user field, the value of which can be set for
* each recipient, and then it can be substituted in the letter.
* @method updateField(array $params) It is a method to change user field parameters.
* @method deleteField(array $params) It is a method to delete a user field.
* @method getTags() It is a method to get list of all tags.
* @method deleteTag(array $params) It is a method to delete a user tag.
* @method isContactInLists(array $params) Checks whether contact is in list.
* @method getContactFieldValues(array $params) Get addinitioan fields values for a contact.
*/
class UnisenderApi
{
/**
* @var string
*/
protected $apiKey;
/**
* @var string
*/
protected $encoding = 'UTF-8';
/**
* @var int
*/
protected $retryCount = 0;
/**
* @var int
*/
protected $timeout;
/**
* @var bool
*/
protected $compression = false;
/**
* Not required argument. Its like UserAgent from browsers. For example, put here: My E-commerce v1.0.
* @var string
*/
protected $platform = '';
/**
* @var string
*/
protected $lang = 'en';
/**
* Allowed languages for api request.
* @var array
*/
protected static $languages = ['en', 'ru', 'ua'];
/**
* UniSender Api constructor
*
* For example:
*
* <pre>
*
* $platform = 'My E-commerce product v1.0';
*
* $UnisenderApi = new UnisenderApi('api key here', 'UTF-8', 4, null, false, $platform);
* $UnisenderApi->sendSms(
* ['phone' => 380971112233, 'sender' => 'SenderName', 'text' => 'Hello World!']
* );
*
* </pre>
*
* @param string $apiKey Provide your api key here.
* @param string $encoding If your current encoding is different from UTF-8, specify it here.
* @param int $retryCount
* @param int $timeout
* @param bool $compression
* @param string $platform Specify your product name, example - My E-commerce v1.0.
*
*/
public function __construct($apiKey, $encoding = 'UTF-8', $retryCount = 4, $timeout = null, $compression = false, $platform = null)
{
$this->apiKey = $apiKey;
$platform = trim((string) $platform);
if (!empty($encoding)) {
$this->encoding = $encoding;
}
if (0 < $retryCount) {
$this->retryCount = $retryCount;
}
if (null !== $timeout) {
$this->timeout = $timeout;
}
if ($compression) {
$this->compression = $compression;
}
if ($platform !== '') {
$this->platform = $platform;
}
}
/**
* @param string $name
* @param array $arguments
*
* @return string
*/
public function __call($name, $arguments)
{
if (!is_array($arguments) || 0 === count($arguments)) {
$params = [];
} else {
$params = $arguments[0];
}
return $this->callMethod($name, $params);
}
/**
* Set desired language for api request.
*
* @param string $language
*
* @return $this|bool
*/
public function setApiHostLanguage($language = '')
{
if (in_array($language, static::$languages, true))
{
$this->lang = $language;
return $this;
}
return false;
}
/**
* @param array $params
*
* @return string
*/
public function subscribe($params)
{
$params = (array) $params;
if (empty($params['request_ip'])) {
$params['request_ip'] = $this->getClientIp();
}
return $this->callMethod('subscribe', $params);
}
/**
* Export of contact data from UniSender.
* Depending on the number of contacts to export, the file may take some time to prepare.
* After the export is ready, it will be sent to the URL specified in the notify_url parameter of
* the async/exportContacts method.
*
* You can also request task status.
*
* @see https://www.unisender.com/en/support/api/partners/exportcontacts/
*
* @param array $params
*
* @return false|string
*/
public function taskExportContacts(array $params)
{
return $this->callMethod('async/exportContacts', $params);
}
/**
* Get a results report of the delivery of messages in the given campaign.
* Depending on the number of recipients in the list, a report on it may be prepared for some time.
* After the report is ready, it will be sent to the URL specified in the notify_url parameter of
* the async/getCampaignDeliveryStats method.
*
* You can also request task status.
*
* @see https://www.unisender.com/en/support/statistics/getcampaigndeliverystats/
*
* @param array $params
*
* @return false|string
*/
public function taskGetCampaignDeliveryStats(array $params)
{
return $this->callMethod('async/getCampaignDeliveryStats', $params);
}
/**
* Get task status
*
* @param array $params
*
* @return false|string
*/
public function getTaskResult(array $params)
{
return $this->callMethod('async/getTaskResult', $params);
}
/**
* The getCurrencyRates method allows you to get a list of all currencies in the UniSender system.
*
* @see https://www.unisender.com/en/support/api/common/getcurrencyrates/
*
* @return false|string
*/
public function getCurrencyRates()
{
return $this->callMethod('getCurrencyRates');
}
/**
* The method sends a message to the email address with a link to confirm the address as the return address.
* After clicking on this link, you can send messages on behalf of this email address.
*
* @param array $params
*
* @see https://www.unisender.com/en/support/api/partners/validatesender/
*
* @return false|string
*/
public function validateSender(array $params)
{
return $this->callMethod('validateSender', $params);
}
/**
* The system will register the domain in the list for authentication and generate a dkim key for it.
* Confirm the address on the domain to add the domain to the list.
*
* @see https://www.unisender.com/en/support/api/partners/setsenderdomain/
*
* @param array $params
*
* @return false|string
*/
public function setSenderDomain(array $params)
{
return $this->callMethod('setSenderDomain', $params);
}
/**
* Get domains list registrated by setSenderDomain api method.
*
* @see https://www.unisender.com/en/support/api/partners/setsenderdomain/
*
* @param array $params
*
* @return false|string
*/
public function getSenderDomainList(array $params)
{
return $this->callMethod('getSenderDomainList', $params);
}
/**
* The method returns an object with confirmed and unconfirmed sender’s addresses. Unconfirmed sender’s address
* is the address to which the message was sent with a link to confirm the return address,
* but the confirmation link wasn’t clicked.
* To verify the return address, you can use the validateSender method.
*
* @see https://www.unisender.com/en/support/api/partners/getcheckedemail/
*
* @param array $params
*
* @return false|string
*/
public function getCheckedEmail(array $params)
{
return $this->callMethod('getCheckedEmail', $params);
}
/**
* This method return information about contact.
*
* @param array $params Array: email, api_key
*
* @return false|string
*/
public function getContact(array $params)
{
return $this->callMethod('getContact', $params);
}
/**
* @param string $json
*
* @return mixed
*/
protected function decodeJSON($json)
{
return json_decode($json);
}
/**
* @return string
*/
protected function getClientIp()
{
$result = '';
if (!empty($_SERVER['REMOTE_ADDR'])) {
$result = $_SERVER['REMOTE_ADDR'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$result = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$result = $_SERVER['HTTP_CLIENT_IP'];
}
if (preg_match('/([0-9]|[0-9][0-9]|[01][0-9][0-9]|2[0-4][0-9]|25[0-5])(\.' .
'([0-9]|[0-9][0-9]|[01][0-9][0-9]|2[0-4][0-9]|25[0-5])){3}/', $result, $match)) {
return $match[0];
}
return $result;
}
/**
* @param string $value
* @param string $key
*/
protected function iconv(&$value, $key)
{
$value = iconv($this->encoding, 'UTF-8//IGNORE', $value);
}
/**
* @param string $value
* @param string $key
*/
protected function mb_convert_encoding(&$value, $key)
{
$value = mb_convert_encoding($value, 'UTF-8', $this->encoding);
}
/**
* @param $methodName
* @param array $params
*
* @return false|string
*/
protected function callMethod($methodName, $params = [])
{
if ($this->platform !== '') {
$params['platform'] = $this->platform;
}
if (strtoupper($this->encoding) !== 'UTF-8') {
if (function_exists('iconv')) {
array_walk_recursive($params, [$this, 'iconv']);
} elseif (function_exists('mb_convert_encoding')) {
array_walk_recursive($params, [$this, 'mb_convert_encoding']);
}
}
$url = $methodName.'?format=json';
if ($this->compression) {
$url .= '&api_key='.$this->apiKey.'&request_compression=bzip2';
$content = bzcompress(http_build_query($params));
} else {
$params = array_merge((array) $params, ['api_key' => $this->apiKey]);
$content = http_build_query($params);
}
$contextOptions = [
'http' => [
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $content,
],
'ssl' => [
'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT,
]
];
if ($this->timeout) {
$contextOptions['http']['timeout'] = $this->timeout;
}
$retryCount = 0;
$context = stream_context_create($contextOptions);
do {
$host = $this->getApiHost();
$result = @file_get_contents($host.$url, false, $context);
++$retryCount;
} while ($result === false && $retryCount < $this->retryCount);
return $result;
}
/**
* @return string
*/
protected function getApiHost()
{
return sprintf('https://api.unisender.com/%s/api/', $this->lang);
}
}