/**
  * @param $api_key
  * @param $account_id
  * @param $email
  * @param $list_id
  * @param $name
  * @param $last_name
  * @param $post_name
  * @param $cookie
  * @return string
  */
 public function submit_hubspot_form($api_key, $account_id, $email, $list_id, $name, $last_name, $post_name, $cookie)
 {
     if (!class_exists('HubSpot_Forms_Rapidology')) {
         include_once RAD_RAPIDOLOGY_PLUGIN_DIR . 'subscription/hubspot/class.forms.php';
     }
     $names_array = rapidology_name_splitter($name, $last_name);
     $name = $names_array['name'];
     $last_name = $names_array['last_name'];
     $submitted_form_fields = array('firstname' => $name, 'lastname' => $last_name, 'email' => $email);
     $context = array('hutk' => $cookie, 'ipAddress' => $_SERVER['REMOTE_ADDR'], 'pageUrl' => $_SERVER['HTTP_HOST'], 'pageName' => $post_name);
     $forms = new HubSpot_Forms_Rapidology($api_key);
     $submitted_form = $forms->submit_form($account_id, $list_id, $submitted_form_fields, $context);
     if ($submitted_form['error']) {
         $error_message = 'There was an error submitting your form';
     } else {
         $error_message = 'success';
     }
     return $error_message;
 }
 public function hubspot_subscribe($api_key, $email, $list_id, $name, $last_name)
 {
     if (!class_exists('HubSpot_Lists_Rapidology')) {
         require_once RAD_RAPIDOLOGY_PLUGIN_DIR . 'subscription/hubspot/class.lists.php';
     }
     if (!class_exists('HubSpot_Contacts_Rapidology')) {
         require_once RAD_RAPIDOLOGY_PLUGIN_DIR . 'subscription/hubspot/class.contacts.php';
     }
     $contacts = new HubSpot_Contacts_Rapidology($api_key);
     $lists = new HubSpot_Lists_Rapidology($api_key);
     //see if contact exists
     $contact_exists = false;
     $contact_id = '';
     $error_message = '';
     $contactByEmail = $contacts->get_contact_by_email($email);
     if (!empty($contactByEmail) && isset($contactByEmail->vid)) {
         $contact_exists = true;
         $contact_id = $contactByEmail->vid;
     }
     //add contact
     if ($contact_exists == false) {
         //try to make a smart guess if they put their first and last name in the name field or if its just a single name form
         $names_array = rapidology_name_splitter($name, $last_name);
         $name = $names_array['name'];
         $last_name = $names_array['last_name'];
         $args = array('email' => $email, 'firstname' => $name, 'lastname' => $last_name);
         $new_contact = $contacts->create_contact($args);
         $contact_id = $new_contact->vid;
     }
     //add contact to list
     $contacts_to_add = array($contact_id);
     $added_contacts = $lists->add_contacts_to_list($contacts_to_add, $list_id);
     $response = json_decode($added_contacts);
     if (!empty($response->updated)) {
         $error_message = 'success';
     } else {
         $error_message = 'Email address already exists in list';
     }
     return $error_message;
 }
 public function rapidology_submit_ac_form($form_id, $first_name, $last_name, $email, $lists_array, $url)
 {
     $this->api_action = 'contact_add';
     $params = array('api_key' => $this->api_key, 'api_action' => $this->api_action, 'api_output' => $this->api_output);
     $names_array = rapidology_name_splitter($first_name, $last_name);
     $first_name = $names_array['name'];
     $last_name = $names_array['last_name'];
     $post_fields = array();
     $post_fields['first_name'] = $first_name;
     $post_fields['last_name'] = $last_name;
     $post_fields['email'] = $email;
     foreach ($lists_array as $list) {
         $post_fields["p[{$list}]"] = $list;
         $post_fields['status'] = 1;
         $post_fields["instantresponders[{$list}]"] = 0;
     }
     $post_fields['form'] = $form_id;
     // This section takes the input fields and converts them to the proper format
     $query = "";
     foreach ($params as $key => $value) {
         $query .= $key . '=' . urlencode($value) . '&';
     }
     $query = rtrim($query, '& ');
     // This section takes the input data and converts it to the proper format
     $data = "";
     foreach ($post_fields as $key => $value) {
         $data .= $key . '=' . urlencode($value) . '&';
     }
     $data = rtrim($data, '& ');
     $url = rtrim($this->url, '/ ');
     if (!function_exists('curl_init')) {
         die('CURL not supported. (introduced in PHP 4.0.2)');
     }
     // If JSON is used, check if json_decode is present (PHP 5.2.0+)
     if ($params['api_output'] == 'json' && !function_exists('json_decode')) {
         die('JSON not supported. (introduced in PHP 5.2.0)');
     }
     // define a final API request - GET
     $api = $url . '/admin/api.php?' . $query;
     $request = curl_init($api);
     // initiate curl object
     curl_setopt($request, CURLOPT_HEADER, 0);
     // set to 0 to eliminate header info from response
     curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
     // Returns response data instead of TRUE(1)
     curl_setopt($request, CURLOPT_POSTFIELDS, $data);
     // use HTTP POST to send form data
     //curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment if you get no gateway response and are using HTTPS
     curl_setopt($request, CURLOPT_FOLLOWLOCATION, true);
     $response = (string) curl_exec($request);
     // execute curl post and store results in $response
     // additional options may be required depending upon your server configuration
     // you can find documentation on curl options at http://www.php.net/curl_setopt
     curl_close($request);
     // close curl object
     if (!$response) {
         die('Nothing was returned. Do you have a connection to Email Marketing server?');
     }
     $results = json_decode($response);
     $success = array();
     if ($results->result_code) {
         $success['result'] = 'success';
         $success['message'] = 'success';
     } else {
         $success['result'] = 'error';
         $success['message'] = 'There seems to be an issue with your form. Please check it for invalid fields.';
     }
     return $success;
 }