You are here

function cc::create_contacts in Constant Contact 6.2

Same name and namespace in other branches
  1. 6.3 class.cc.php \cc::create_contacts()
  2. 7.3 class.cc.php \cc::create_contacts()

* This method is used to add 25 or more contacts * Pass this method an associative array of contact details * Alternatively you can give a path to a local or remote file * The file should be text or CSV format: * * * *

Parameters

mixed This can be an array or a path to a file: * @param array An array of contact list ID's to add the user to * * @access public

See also

http://constantcontact.custhelp.com/cgi-bin/constantcontact.cfg/php/endu...

File

./class.cc.php, line 1000

Class

cc
@file

Code

function create_contacts($contacts, $lists) {
  $params['activityType'] = 'SV_ADD';
  if (is_array($contacts) && count($contacts) > 0) {

    // get fieldnames from keys of the first contact array
    $fieldnames = array_keys($contacts[0]);
    $contacts = array_values($contacts);

    // transform the given array into a CSV formatted string
    $contacts_string = '';
    foreach ($contacts as $k => $contact) {
      foreach ($fieldnames as $k => $fieldname) {
        if (isset($contact[$fieldname]) || is_null($contact[$fieldname])) {
          $contacts_string .= $contact[$fieldname] . "{$this->http_linebreak}";
        }
        else {
          $this->last_error = 'contacts array is not formatted correctly, please ensure all contact entries have the same fields and values';
          return false;
        }
      }
    }
    $params['data'] = implode(',', $fieldnames) . "{$this->http_linebreak}" . $contacts_string;
    $lists_string = '';
    if (is_array($lists)) {
      foreach ($lists as $id) {
        $params['lists'][] = $this
          ->get_list_url($id);
      }
    }
  }
  elseif (file_exists($contacts) && is_readable($contacts)) {

    // grab the file and output it directly in the request
    $params['data'] = file_get_contents($contacts);
  }
  $this
    ->http_set_content_type('application/x-www-form-urlencoded');
  $this
    ->load_url("activities", 'post', $params, 201);
  if (isset($this->http_response_headers['Location']) && trim($this->http_response_headers['Location']) != '') {
    return $this
      ->get_id_from_link($this->http_response_headers['Location']);
  }
  return false;
}