-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRESTAPIphpListClient.php
More file actions
101 lines (82 loc) · 2.46 KB
/
RESTAPIphpListClient.php
File metadata and controls
101 lines (82 loc) · 2.46 KB
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
<?php
/**
* A very simple example of REST API client of phpList
* @author Xheni Myrtaj
**/
require __DIR__ . '/vendor/autoload.php';
$client = new \GuzzleHttp\Client();
//Please replace the following values with yours.
$loginname = 'admin';
$password = 'phplist';
$base_uri = 'http://example.com/lists/api/v2';
try {
$response = $client->request('POST', $base_uri . '/sessions', [
'form_params' => [
'login_name' => $loginname,
'password' => $password,
],
]);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
}
//get session key
if ($response->getBody()) {
$obj = json_decode($response->getBody(), true);
$key = $obj['key'];
echo 'Session key is: ' . $key . '<br><br>';
}
//Use session key as password for basic auth
$credentials = base64_encode($loginname . ':' . $key);
// Get list info where id=1
$listInfo = $client->get($base_uri . '/lists/1',
[
'headers' => [
'Authorization' => 'Basic ' . $credentials,
'Content-Type' => 'application/json',
],
]);
if ($listInfo->getBody()) {
$listInfoResponse = json_decode($listInfo->getBody(), true);
echo 'List Info: <br><br>';
foreach ($listInfoResponse as $key => $value) {
echo "$key : $value<br>";
}
echo '<br>';
}
//Get all subscribers where list id=1
$members = $client->get($base_uri . '/lists/1/members',
[
'headers' => [
'Authorization' => 'Basic ' . $credentials,
'Content-Type' => 'application/json',
],
]);
if ($members->getBody()) {
$membersResponse = json_decode($members->getBody(), true);
echo 'Subscribers of ' . $listInfoResponse['name'] . ':<br><br>';
foreach ($membersResponse as $k => $val) {
foreach ($val as $key => $value) {
echo "$key : $value<br>";
}
echo '<br>';
}
}
// Add a new subscriber
try {
$subscriberRequest = $client->request('POST', $base_uri . '/subscribers',
[
'headers' => [
'Authorization' => 'Basic ' . $credentials,
'Content-Type' => 'application/json',
],
'json' => [
'email' => 'restapi@example.com',
'confirmed' => true,
'blacklisted' => false,
'html_email' => true,
'disabled' => false,
],
]
);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
}
$subscriberRequest->getBody();