-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrevue-to-encharge.php
263 lines (214 loc) · 9.13 KB
/
revue-to-encharge.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
<?php
/**
* This file will allow the syncing of new subscribers from Revue (https://www.getrevue.co/) to Encharge (https://encharge.io/)
*
* Upon running it should check the subscribers in Revue and synchronize (add or update them) into Encharge.
*
* If a user is already in Encharge it should update the user unless the user has unsubscribed in Encharge,
* then the user should be unsubscribed in Revue as well.
*/
/* ---------------------------------------------------------------------------
* Setup API keys and Communication Category ID's (Encharge) needed for syncing
*
* Revue API key can be found here: https://www.getrevue.co/app/integrations
* Encharge API key can be found here: https://app.encharge.io/account/info
* Encharge Communication Category ID's can be found here: https://app.encharge.io/settings/person-fields?personfields-folder-item=CommunicationCategories
*
* --------------------------------------------------------------------------- */
$revue_api_key = 'replace_me_with_your_revue_api_key';
$encharge_api_key = 'replace_me_with_your_encharge_api_key';
$encharge_marketing_emails_category_id = 'replace_me_with_your_marketing_email_category_id';
$encharge_transactional_emails_category_id = 'replace_me_with_your_transactional_email_category_id';
/* ---------------------------------------------------------------------------
* Main class
* --------------------------------------------------------------------------- */
class RevueToEncharge
{
protected $revue_api_key;
protected $encharge_api_key;
protected $encharge_marketing_emails_category_id;
protected $encharge_transactional_emails_category_id;
public function __construct($revue_api_key, $encharge_api_key, $encharge_marketing_emails_category_id, $encharge_transactional_emails_category_id)
{
$this->revue_api_key = $revue_api_key;
$this->encharge_api_key = $encharge_api_key;
$this->encharge_marketing_emails_category_id = $encharge_marketing_emails_category_id;
$this->encharge_transactional_emails_category_id = $encharge_transactional_emails_category_id;
}
public function getSubscriberRevue()
{
$curl = curl_init();
$headers = array(
"Authorization: Bearer {$this->revue_api_key}",// send token in Bearer header request
"accept: application/json"
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_URL,"https://www.getrevue.co/api/v2/subscribers");
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_USERAGENT => 'Test',
CURLOPT_SSL_VERIFYPEER => false
));
$list_user_subscribers = curl_exec($curl);
curl_close($curl);
return $list_user_subscribers;
}
public function unsubscribesRevue($email)
{
$curl = curl_init();
$fields = ([
"email" => $email
]);
curl_setopt(
$curl,
CURLOPT_HTTPHEADER,
array(
"Authorization: Bearer {$this->revue_api_key}",// send token in Bearer header request
"accept: application/json"
)
);
curl_setopt($curl, CURLOPT_URL,"https://www.getrevue.co/api/v2/subscribers/unsubscribe");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$server_output = curl_exec($curl);
curl_close($curl);
return true;
}
public function getUserEncharge($userId)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.encharge.io/v1/people?people[0][userId]=". $userId);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'GET');
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json', // for define content type that is json
'X-Encharge-Token: '.$this->encharge_api_key.'', // send token in header request
)
);
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_USERAGENT => 'Test',
CURLOPT_SSL_VERIFYPEER => false
));
$user = curl_exec($ch);
curl_close($ch);
return $user;
}
public function addToEncharge($user)
{
$ch = curl_init();
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json', // define content type that is json
'X-Encharge-Token: '.$this->encharge_api_key.'', // send token in header request
)
);
$fields = json_encode([
"name" => $user->first_name. ' ' . $user->last_name,
"email" => $user->email ,
"firstName" => $user->first_name,
"lastName" => $user->last_name,
"userId" => $user->id,
'SOURCE' => 'Twitter Subscriber via Revue',
'tags' => 'Twitter/Revue Subscriber',
'CommunicationCategories.cat_'.$this->encharge_marketing_emails_category_id.'' => 'Opted in',
'CommunicationCategories.cat_'.$this->encharge_transactional_emails_category_id.'' => 'Opted in'
]);
curl_setopt($ch, CURLOPT_URL,"https://api.encharge.io/v1/people");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close($ch);
return true;
}
public function UpdateEncharge($user)
{
$ch = curl_init();
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json', // define content type that is json
'X-Encharge-Token: '.$this->encharge_api_key.'', // send token in header request
)
);
$fields = json_encode([
"name" => $user->first_name. ' ' . $user->last_name,
"email" => $user->email ,
"firstName" => $user->first_name,
"lastName" => $user->last_name,
"userId" => $user->id,
'CommunicationCategories.cat_'.$this->encharge_marketing_emails_category_id.'' => 'Opted in',
'CommunicationCategories.cat_'.$this->encharge_transactional_emails_category_id.'' => 'Opted in'
]);
curl_setopt($ch, CURLOPT_URL,"https://api.encharge.io/v1/people");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close($ch);
return true;
}
public function debug($message) {
return file_put_contents('debug.log', "\n $message" , FILE_APPEND);
}
public function sync()
{
$list_sub = $this->getSubscriberRevue();
$list_sub = json_decode($list_sub);
if (count($list_sub) < 1) {
$error = "syncing error: you don't have any subscribers on Revue";
$this->debug($error);
}
$countAddToEncharge = 0;
$countUnsubscribes = 0;
$date = date("Y/m/d h:i:sa");
foreach ($list_sub as $sub) {
$user_encharge = ($this->getUserEncharge($sub->id));
$user_encharge = json_decode($user_encharge);
if (!$user_encharge->users) {
$this->addToEncharge($sub);
$countAddToEncharge += 1;
$message = "$date Added $sub->email to Encharge from Revue";
$this->debug($message);
continue;
}
if ($user_encharge->users) {
if ($sub->first_name != $user_encharge->users[0]->firstName || $sub->last_name!=$user_encharge->users[0]->lastName ) {
if ($user_encharge->users[0]->SOURCE) {
$this->UpdateEncharge($sub);
} else {
$this->addToEncharge($sub);
}
$countAddToEncharge += 1;
$message = "$date Updated $sub->email in Encharge with new Revue data";
$this->debug($message);
}
}
if ($user_encharge->users[0]->unsubscribed == true) {
$this->unsubscribesRevue($user_encharge->users[0]->email);
$message = "$date Unsubscribed $sub->email from Revue";
$this->debug($message);
$countUnsubscribes += 1;
continue;
}
}
if ($countAddToEncharge == 0 && $countUnsubscribes == 0) {
$message = "$date Nothing to update";
$this->debug($message);
}
echo "Sync successfully!!\n";
}
}
/* ---------------------------------------------------------------------------
* Run the script
* --------------------------------------------------------------------------- */
$app = new RevueToEncharge($revue_api_key, $encharge_api_key, $encharge_marketing_emails_category_id, $encharge_transactional_emails_category_id);
$app->sync();
?>