CampaignMonitor::getUnsubscribes PHP Method

getUnsubscribes() public method

Contains a paged result representing all the unsubscribed subscribers for a given list. This includes their email address, name, date unsubscribed and any custom field data. You have complete control over how results should be returned including page sizes, sort order and sort direction.
public getUnsubscribes ( string[optional] $listId = null, int[optional] $timestamp = null, int[optional] $page = 1, int[optional] $pageSize = 1000, string[optional] $orderField = 'email', string[optional] $orderDirection = 'asc' ) : array
$listId string[optional]
$timestamp int[optional]
$page int[optional]
$pageSize int[optional]
$orderField string[optional]
$orderDirection string[optional]
return array
    public function getUnsubscribes($listId = null, $timestamp = null, $page = 1, $pageSize = 1000, $orderField = 'email', $orderDirection = 'asc')
    {
        // set ID
        $listId = empty($listId) ? $this->getListId() : $listId;
        // check timestamp
        if (empty($timestamp)) {
            $timestamp = strtotime('last week');
        }
        // set parameters
        $parameters['date'] = (string) date('Y-m-d', $timestamp);
        $parameters['page'] = (int) $page;
        $parameters['pagesize'] = (int) $pageSize;
        $parameters['orderfield'] = (string) $orderField;
        $parameters['orderdirection'] = (string) $orderDirection;
        // make the call
        $records = (array) $this->doCall('lists/' . $listId . '/unsubscribed', $parameters);
        // stop here if no records were set
        if (empty($records['Results'])) {
            return array();
        }
        // reserve variables
        $results = array();
        // loop the records
        foreach ($records['Results'] as $key => $record) {
            // set values
            $results[$key]['email'] = $record['EmailAddress'];
            $results[$key]['name'] = $record['Name'];
            $results[$key]['date'] = $record['Date'];
            $results[$key]['status'] = $record['State'];
            $results[$key]['custom_fields'] = array();
            // check if there are clickedlinks present
            if (empty($record['CustomFields'])) {
                continue;
            }
            // loop records
            foreach ($record['CustomFields'] as $field) {
                // set values
                $results[$key]['custom_fields'][$field['Key']] = $field['Value'];
            }
        }
        // return the records
        return $results;
    }