-
Notifications
You must be signed in to change notification settings - Fork 11
/
phpListRESTApiClient.php
360 lines (318 loc) · 10 KB
/
phpListRESTApiClient.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
<?php
/**
*
* Class phpListRESTApiClient.
*
*/
/**
*
* example PHP client class to access the phpList Rest API.
* License: MIT, https://opensource.org/licenses/MIT
*
* To use this class, you need the restapi plugin for phpList, https://resources.phplist.com/plugin/restapi
* Set the parameters below to match your system:
*
* - url : URL of your phpList installation
* - loginName : admin login
* - password : matching password
* - remoteProcessingSecret : (optional) the secret as defined in your phpList settings
*
* v 1.01 Nov 26, 2015 added optional secret on instantiation
* v 1 * Michiel Dethmers, phpList Ltd, November 18, 2015
* Initial implementation of basic API calls
*/
class phpListRESTApiClient
{
/**
* URL of the API to connect to including the path
* generally something like.
*
* https://website.com/lists/admin/?pi=restapi&page=call
*/
private $url;
/**
* login name for the phpList installation.
*/
private $loginName;
/**
* password to login.
*/
private $password;
/**
* the path where we can write our cookiejar.
*/
public $tmpPath = '/tmp';
/**
* optionally the remote processing secret of the phpList installation
* this will increase the security of the API calls.
*/
private $remoteProcessingSecret;
/**
* construct, provide the Credentials for the API location.
*
* @param string $url URL of the API
* @param string $loginName name to login with
* @param string $password password for the account
* @return null
*/
public function __construct($url, $loginName, $password, $secret = '')
{
$this->url = $url;
$this->loginName = $loginName;
$this->password = $password;
$this->remoteProcessingSecret = $secret;
}
/**
* Make a call to the API using cURL.
*
* @param string $command The command to run
* @param array $post_params Array for parameters for the API call
* @param bool $decode json_decode the result (defaults to true)
*
* @return string result of the CURL execution
*/
private function callApi($command, $post_params, $decode = true, $newSession = false)
{
$post_params['cmd'] = $command;
// optionally add the secret to a call, if provided
if (!empty($this->remoteProcessingSecret)) {
$post_params['secret'] = $this->remoteProcessingSecret;
}
$post_params = http_build_query($post_params);
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $this->url);
curl_setopt($c, CURLOPT_HEADER, 0);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, $post_params);
curl_setopt($c, CURLOPT_COOKIEFILE, $this->tmpPath.'/phpList_RESTAPI_cookiejar.txt');
curl_setopt($c, CURLOPT_COOKIEJAR, $this->tmpPath.'/phpList_RESTAPI_cookiejar.txt');
curl_setopt($c, CURLOPT_COOKIESESSION, $newSession); // Fix for random failed authentication
curl_setopt($c, CURLOPT_HTTPHEADER, array('Connection: Keep-Alive', 'Keep-Alive: 60'));
// Execute the call
$result = curl_exec($c);
// Check if decoding of result is required
if ($decode === true) {
$result = json_decode($result);
}
return $result;
}
/**
* Use a real login to test login api call.
*
* @param none
*
* @return bool true if user exists and login successful
*/
public function login()
{
// Set the username and pwd to login with
$post_params = array(
'login' => $this->loginName,
'password' => $this->password,
);
// Execute the login with the credentials as params
$result = $this->callApi('login', $post_params, true, true);
return $result->status == 'success';
}
/**
* Create a list.
*
* @param string $listName Name of the list
* @param string $listDescription Description of the list
*
* @return int ListId of the list created
*/
public function listAdd($listName, $listDescription)
{
// Create minimal params for api call
$post_params = array(
'name' => $listName,
'description' => $listDescription,
'listorder' => '0',
'active' => '1',
);
// Execute the api call
$result = $this->callAPI('listAdd', $post_params);
// get the ID of the list we just created
$listId = $result->data->id;
return $listId;
}
/**
* Get all lists.
*
* @return array All lists
*/
public function listsGet()
{
// Create minimal params for api call
$post_params = array(
);
// Execute the api call
$result = $this->callAPI('listsGet', $post_params);
// Return all list as array
return $result->data;
}
/**
* Find a subscriber by email address.
*
* @param string $emailAddress Email address to search
*
* @return int $subscriberID if found false if not found
*/
public function subscriberFindByEmail($emailAddress)
{
$params = array(
'email' => $emailAddress,
);
$result = $this->callAPI('subscriberGetByEmail', $params);
if (!empty($result->data->id)) {
return $result->data->id;
} else {
return false;
}
}
/**
* Add a subscriber.
*
* This is the main method to use to add a subscriber. It will add the subscriber as
* a non-confirmed subscriber in phpList and it will send the Request-for-confirmation
* email as set up in phpList.
*
* The lists parameter will set the lists the subscriber will be added to. This has
* to be comma-separated list-IDs, eg "1,2,3,4".
*
* @param string $emailAddress email address of the subscriber to add
* @param string $lists comma-separated list of IDs of the lists to add the subscriber to
*
* @return int $subscriberId if added, or false if failed
*/
public function subscribe($emailAddress, $lists)
{
// Set the user details as parameters
$post_params = array(
'email' => $emailAddress,
'foreignkey' => '',
'htmlemail' => 1,
'subscribepage' => 0,
'lists' => $lists,
);
// Execute the api call
$result = $this->callAPI('subscribe', $post_params);
if (!empty($result->data->id)) {
$subscriberId = $result->data->id;
return $subscriberId;
} else {
return false;
}
}
/**
* Fetch subscriber by ID.
*
* @param int $subscriberID ID of the subscriber
*
* @return the subscriber
*/
public function subscriberGet($subscriberId)
{
$post_params = array(
'id' => $subscriberId,
);
// Execute the api call
$result = $this->callAPI('subscriberGet', $post_params);
if (!empty($result->data->id)) {
$fetchedSubscriberId = $result->data->id;
$this->assertEquals($fetchedSubscriberId, $subscriberId);
return $result->data;
} else {
return false;
}
}
/**
* Get a subscriber by Foreign Key.
*
* Note the difference with subscriberFindByEmail which only returns the SubscriberID
* Both API calls return the subscriber
*
* @param string $foreignKey Foreign Key to search
*
* @return subscriber object if found false if not found
*/
public function subscriberGetByForeignkey($foreignKey)
{
$post_params = array(
'foreignkey' => $foreignKey,
);
$result = $this->callAPI('subscriberGetByForeignkey', $post_params);
if (!empty($result->data->id)) {
return $result->data;
} else {
return false;
}
}
/**
* Get the total number of subscribers.
*
* @param none
*
* @return int total number of subscribers in the system
*/
public function subscriberCount()
{
$post_params = array(
);
$result = $this->callAPI('subscribersCount', $post_params);
return $result->data->total;
}
/**
* Add a subscriber to an existing list.
*
* @param int $listId ID of the list
* @param int $subscriberId ID of the subscriber
*
* @return the lists this subscriber is member of
*/
public function listSubscriberAdd($listId, $subscriberId)
{
// Set list and subscriber vars
$post_params = array(
'list_id' => $listId,
'subscriber_id' => $subscriberId,
);
$result = $this->callAPI('listSubscriberAdd', $post_params);
return $result->data;
}
/**
* Get the lists a subscriber is member of.
*
* @param int $subscriberId ID of the subscriber
*
* @return the lists this subcriber is member of
*/
public function listsSubscriber($subscriberId)
{
$post_params = array(
'subscriber_id' => $subscriberId,
);
$result = $this->callAPI('listsSubscriber', $post_params);
return $result->data;
}
/**
* Remove a Subscriber from a list.
*
* @param int $listId ID of the list to remove
* @param int $subscriberId ID of the subscriber
*
* @return the lists this subcriber is member of
*/
public function listSubscriberDelete($listId, $subscriberId)
{
// Set list and subscriber vars
$post_params = array(
'list_id' => $listId,
'subscriber_id' => $subscriberId,
);
$result = $this->callAPI('listSubscriberDelete', $post_params);
return $result->data;
}
}