-
Notifications
You must be signed in to change notification settings - Fork 42
/
manage-subscriber
executable file
·105 lines (94 loc) · 3.73 KB
/
manage-subscriber
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
#!/usr/bin/env php
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
const BASE_URL = 'https://api.aweber.com/1.0/';
// Create a Guzzle client
$client = new GuzzleHttp\Client();
// Load credentials
$credentials = parse_ini_file('credentials.ini');
$accessToken = $credentials['accessToken'];
/**
* Get all of the entries for a collection
*
* @param Client $client HTTP Client used to make a GET request
* @param string $accessToken Access token to pass in as an authorization header
* @param string $url Full url to make the request
* @return array Every entry in the collection
*/
function getCollection($client, $accessToken, $url) {
$collection = array();
while (isset($url)) {
$request = $client->get($url,
['headers' => ['Authorization' => 'Bearer ' . $accessToken]]
);
$body = $request->getBody();
$page = json_decode($body, true);
$collection = array_merge($page['entries'], $collection);
$url = isset($page['next_collection_link']) ? $page['next_collection_link'] : null;
}
return $collection;
}
// get all the accounts entries
$accounts = getCollection($client, $accessToken, BASE_URL . 'accounts');
$accountUrl = $accounts[0]['self_link'];
// get all the list entries for the first account
$listsUrl = $accounts[0]['lists_collection_link'];
$lists = getCollection($client, $accessToken, $listsUrl);
// find out if a subscriber exists on the first list
$email = 'example@example.com';
$params = array(
'ws.op' => 'find',
'email' => $email
);
$subsUrl = $lists[0]['subscribers_collection_link'];
$findUrl = $subsUrl . '?' . http_build_query($params);
$foundSubscribers = getCollection($client, $accessToken, $findUrl);
echo 'Found subscribers: ';
print_r($foundSubscribers);
if (isset($foundSubscribers[0]['self_link'])) {
// update the subscriber if they are on the first list
$data = array(
'custom_fields' => array('awesomeness' => 'really awesome'),
'tags' => array('add' => array('prospect'))
);
$subscriberUrl = $foundSubscribers[0]['self_link'];
$subscriberResponse = $client->patch($subscriberUrl, [
'json' => $data,
'headers' => ['Authorization' => 'Bearer ' . $accessToken]
])->getBody();
$subscriber = json_decode($subscriberResponse, true);
echo 'Updated Subscriber: ';
} else {
// add the subscriber if they are not already on the first list
$data = array(
'email' => $email,
'custom_fields' => array('awesomeness' => 'somewhat'),
'tags' => array('prospect')
);
$body = $client->post($subsUrl, [
'json' => $data,
'headers' => ['Authorization' => 'Bearer ' . $accessToken]
]);
// get the subscriber entry using the Location header from the post request
$subscriberUrl = $body->getHeader('Location')[0];
$subscriberResponse = $client->get($subscriberUrl,
['headers' => ['Authorization' => 'Bearer ' . $accessToken]])->getBody();
$subscriber = json_decode($subscriberResponse, true);
echo 'Created Subscriber: ';
}
print_r($subscriber);
// get the activity for the subscriber
$params = array('ws.op' => 'getActivity');
$activityUrl = $subscriberUrl . '?' . http_build_query($params);
$activity = $client->get($activityUrl,
['headers' => ['Authorization' => 'Bearer ' . $accessToken]])->getBody();
echo 'Subscriber Activity: ';
print_r(json_decode($activity, true));
// delete the subscriber; this can only be performed on confirmed subscribers
// or a 405 Method Not Allowed will be returned
if ($subscriber['status'] == 'subscribed') {
$client->delete($subscriberUrl,
['headers' => ['Authorization' => 'Bearer ' . $accessToken]])->getBody();
echo 'Deleted subscriber with email: ' . $email;
}