You are here

function cc::create_contacts in Constant Contact 7.3

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

Bulk 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 the path to a local or remote file. The file should be text or CSV format: @link http://constantcontact.custhelp.com/cgi-bin/constantcontact.cfg/php/endu...

@access public

Parameters

mixed This can be an array or a path to a file:

array An array of contact list ID's to add the user to:

File

./class.cc.php, line 1171
Constant Contact PHP Class

Class

cc
@file Constant Contact PHP Class

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] . ",";
        }
        else {
          $this->last_error = 'contacts array is not formatted correctly, please ensure all contact entries have the same fields and values';
          return FALSE;
        }
      }
      $contacts_string .= "{$this->http_linebreak}";
    }
    $params['data'] = implode(',', $fieldnames) . "{$this->http_linebreak}" . $contacts_string;
  }
  elseif (file_exists($contacts) && is_readable($contacts)) {

    // grab the file and output it directly in the request
    $params['data'] = file_get_contents($contacts);
  }
  if (is_array($lists)) {
    foreach ($lists as $id) {
      $params['lists'][] = $this
        ->get_list_url($id);
    }
  }
  $this
    ->http_set_content_type('application/x-www-form-urlencoded');
  $xml = $this
    ->load_url("activities", 'post', $params, 201);
  if ($xml) {
    return TRUE;
  }

  /*
  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;
}