Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 105 lines (94 sloc) 3.73 KB
#!/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;
}
You can’t perform that action at this time.