CampaignMonitor::addSubscriber PHP Method

addSubscriber() public method

Adds a subscriber to an existing subscriber list. If $resubscribe is set to true, it will resubscribe the e-mail address if not active.
public addSubscriber ( string $email, string $name, array[optional] $customFields = [], bool[optional] $resubscribe = true, string[optional] $listId = null ) : boolean
$email string The email address of the new subscriber.
$name string The name of the new subscriber. If the name is unknown, an empty string can be passed in.
$customFields array[optional]
$resubscribe bool[optional]
$listId string[optional]
return boolean
    public function addSubscriber($email, $name, $customFields = array(), $resubscribe = true, $listId = null)
    {
        // set list ID
        $listId = empty($listId) ? $this->getListId() : $listId;
        // set parameters
        $parameters['EmailAddress'] = (string) $email;
        $parameters['Name'] = (string) $name;
        $parameters['Resubscribe'] = (bool) $resubscribe;
        // set custom fields if any were found
        if (!empty($customFields)) {
            // fetch all existing custom fields
            //$currentFields = (array) $this->getCustomFields($listId);
            // loop the custom fields, build a new array
            //foreach($currentFields as $key => $field) $currentFields[$key] = $field['name'];
            // loop the fields
            foreach ($customFields as $key => $value) {
                // check if this field already exists; if not, add it.
                //if(!in_array($key, $currentFields)) $this->createCustomField($key, 'text', null, $listId);
                // add it to the list of field values
                $parameters['CustomFields'][] = array('Key' => $key, 'Value' => $value);
            }
        }
        // make the call
        $this->doCall('subscribers/' . $listId, $parameters, 'POST');
        // if we made it here, return true
        return true;
    }