Home Forums Newsletter Plugin Support Newsletter API Hubspot Integration

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #63427
    Cozmoz
    Participant

    Hi,

    I am using the newsletter-api plugin and have modified the add.php file as follows to integrate into HubSpot. Is there a way to test if this is working? I have tried subscribing with a new user and have not had anything show in HubSpot. I am not receiving any error messages in my Apache log files neither.

    I have a similar script running and working elsewhere on the site.

    Please find the updated code below:

    <?php
    
    include '../../../wp-load.php';
    
    $module = NewsletterApi::$instance;
    $newsletter = Newsletter::instance();
    
    $key = stripslashes($_REQUEST['nk']);
    if (empty($module->options['key']) || $key != $module->options['key']) {
        die('Wrong API key');
    }
    
    if (!$newsletter->is_email($_REQUEST['ne'])) {
        die('Wrong email');
    }
    
    $subscriber = array();
    $subscriber['name'] = stripslashes($_REQUEST['nn']);
    $subscriber['surname'] = stripslashes($_REQUEST['ns']);
    $subscriber['email'] = $newsletter->normalize_email(stripslashes($_REQUEST['ne']));
    
    //BOF: Send Contact To HubSpot
    //Registering User - Send Contact To HubSpot
    $hapikey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    $user_id = get_current_user_id();
    
    $firstname = $subscriber['name'];
    $lastname = $subscriber['surname'];
    $email = $subscriber['email'];
    
    //Hubspot Array
      $arr = array(
        'properties' => array(
          array(
            'property' => 'email',
            'value' => $email
          ),
          array(
            'property' => 'firstname',
            'value' => $firstname
          ),
          array(
            'property' => 'lastname',
            'value' => $lastname
          )
        )
      );
         
      $post_json = json_encode($arr);
      $endpoint = 'https://api.hubapi.com/contacts/v1/contact?hapikey=' . $hapikey;
      $ch = @curl_init();
      @curl_setopt($ch, CURLOPT_POST, true);
      @curl_setopt($ch, CURLOPT_POSTFIELDS, $post_json);
      @curl_setopt($ch, CURLOPT_URL, $endpoint);
      @curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
      @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       
      $response = @curl_exec($ch);
      $status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
      $curl_errors = curl_error($ch);
      @curl_close($ch);
      //echo "curl Errors: " . $curl_errors;
      //echo "\nStatus code: " . $status_code;
      //echo "\nResponse: " . $response;
    //EOF: Send Contact To HubSpot
    
    if (is_array($_REQUEST['nl'])) {
      foreach ($_REQUEST['nl'] as $add_list) {
        $subscriber['list_' . $add_list] = 1;
      }
    }
    else if (!empty($_REQUEST['nl'])) {
      $add_lists = explode('|', $_REQUEST['nl']);
      foreach ($add_lists as $add_list) {
        $subscriber['list_' . $add_list] = 1;
      }
    }
    
    $options_feed = get_option('newsletter_feed', array());
    if ($options_feed['add_new'] == 1) $subscriber['feed'] = 1;
    
    $options_followup = get_option('newsletter_followup', array());
    if ($options_followup['add_new'] == 1) {
      $subscriber['followup'] = 1;
      $subscriber['followup_time'] = time() + $options_followup['interval'] * 3600;
    }
    
    $subscriber['status'] = 'C';
    
    // TODO: add control for already subscribed emails
    NewsletterUsers::instance()->save_user($subscriber);
    die('ok');

    Many thanks,

    Costa

    #64007
    Stefano
    Keymaster

    You should check the error returned by curl call.

    Stefano.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.