This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEmarsysClient.php
86 lines (63 loc) · 2.69 KB
/
EmarsysClient.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
<?php
namespace SampleIntegration;
use GuzzleHttp\Client;
class EmarsysClient
{
private $customerId;
private $baseUrl;
private $escherKeyId;
private $escherSecret;
private $escher;
private $restClient;
public function __construct($customerId)
{
$this->customerId = $customerId;
$this->baseUrl = getenv('SUITE_URL');
$this->escherKeyId = getenv('ESCHER_KEY_ID');
$this->escherSecret = getenv('ESCHER_SECRET');
$this->escher = EscherFactory::createForSuite();
$this->restClient = new Client(['base_uri' => $this->baseUrl]);
}
public function getSettings()
{
$uri = "/api/v2/internal/{$this->customerId}/settings";
$headers = $this->escher->signRequest($this->escherKeyId, $this->escherSecret, 'GET', $this->baseUrl . $uri, '');
$response = $this->restClient->request('GET', $uri, ['headers' => $headers]);
return json_decode($response->getBody(), true);
}
public function callbackAutomationCenter($triggerId, $contactId = 0, $contactListId = 0)
{
$uri = "/api/v2/internal/{$this->customerId}/ac/programs/callbacks/{$triggerId}";
$postParams = [
'user_id' => $contactId,
'list_id' => $contactListId
];
$headers = $this->escher->signRequest($this->escherKeyId, $this->escherSecret, 'POST', $this->baseUrl . $uri, json_encode($postParams));
$this->restClient->request('POST', $uri, [
'headers' => $headers,
'body' => json_encode($postParams)
]);
}
public function listContacts()
{
$uri = "/api/v2/internal/{$this->customerId}/contact/query/?return=3&limit=100";
$headers = $this->escher->signRequest($this->escherKeyId, $this->escherSecret, 'GET', $this->baseUrl . $uri, '');
$response = $this->restClient->request('GET', $uri, ['headers' => $headers]);
$parsedResponse = json_decode($response->getBody(), true);
return array_map(function ($contact) {
return ['id' => $contact['id'], 'email' => $contact['3']];
}, $parsedResponse['data']['result']);
}
public function startAutomationCenterPrograms($resourceId, $contactId)
{
$uri = "/api/v2/internal/{$this->customerId}/ac/programs/entrypoints/sample-entry/resources/{$resourceId}/runs";
$postParams = [
'contact_id' => $contactId
];
$headers = $this->escher->signRequest($this->escherKeyId, $this->escherSecret, 'POST', $this->baseUrl . $uri, json_encode($postParams));
$this->restClient->request('POST', $uri, [
'headers' => $headers,
'body' => json_encode($postParams)
]);
}
}