Create Lead and Follow-up Events

As a supplement to the Heap’s Simple API documentation, I want to cover a practical example of using PHP to create a lead and setting up a couple of followups. This blog assumes you have read the API and you have some PHP experience.

First, I want to reduce redundancy in my code, so I’m going to setup a single function that I use to communicate with Heap:

Code:

<?php
function SendtoHeap ($whattosend){
    $URL = "https://wbp.crmondemand.biz/api.php";
    $email = "ben@wbpsystems.com";
    $password = "password";
    $stringtosend = "login_email=" . urlencode($email) . "&login_password=" . urlencode($password);

    foreach($whattosend as $key=>$value){
        $stringtosend = $stringtosend . "&" . $key . "=" . urlencode($value);
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$URL);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $stringtosend);
    $storage = curl_exec ($ch);
    curl_close ($ch);
    return $storage;
}
?>

Ok, so there is a lot in there, but here is what’s going on. First, email, url and password never change when I communicate with heap, so those are just set in the function. Second, the array that is passed is looped through and turned into a string that then can be sent with cURL. Finally Heap’s response is returned.

Creating the Lead

First, I’m going to put the data into an array with each key representing the name of the field in Heap’s API. You could specify many more variables, but I’ve chosen to just specify four. In this scenario, I’m assuming you are getting the info from a submitted form, but this data could be coming from anywhere.

Code:

<?php
function SendtoHeap ($whattosend){
    $URL = "https://wbp.crmondemand.biz/api.php";
    $email = "ben@wbpsystems.com";
    $password = "password";
    $stringtosend = "login_email=" . urlencode($email) . "&login_password=" . urlencode($password);

    foreach($whattosend as $key=>$value){
        $stringtosend = $stringtosend . "&" . $key . "=" . urlencode($value);
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$URL);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $stringtosend);
    $storage = curl_exec ($ch);
    curl_close ($ch);
    return $storage;
}
$leaddata = array();
$leaddata['create'] = "lead";
$leaddata['name'] = trim($_POST['name']);
$leaddata['phone'] = trim($_POST['phone']);
$leaddata['email'] = trim($_POST['email']);
$leaddata['moreinfo'] = trim($_POST['moreinfo']);
?>

I’m going to also specify a couple of categories for these types of leads. Notice how I use the newline to specify two categories.

Code:

<?php
function SendtoHeap ($whattosend){
    $URL = "https://wbp.crmondemand.biz/api.php";
    $email = "ben@wbpsystems.com";
    $password = "password";
    $stringtosend = "login_email=" . urlencode($email) . "&login_password=" . urlencode($password);

    foreach($whattosend as $key=>$value){
        $stringtosend = $stringtosend . "&" . $key . "=" . urlencode($value);
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$URL);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $stringtosend);
    $storage = curl_exec ($ch);
    curl_close ($ch);
    return $storage;
}
$leaddata = array();
$leaddata['create'] = "lead";
$leaddata['name'] = trim($_POST['name']);
$leaddata['phone'] = trim($_POST['phone']);
$leaddata['email'] = trim($_POST['email']);
$leaddata['moreinfo'] = trim($_POST['moreinfo']);

$leaddata['category'] = "Source: Website\nUnprocessed";
?>

Note: Category names must match exactly.

Finally I send the data to our function:

Code:

<?php
function SendtoHeap ($whattosend){
    $URL = "https://wbp.crmondemand.biz/api.php";
    $email = "ben@wbpsystems.com";
    $password = "password";
    $stringtosend = "login_email=" . urlencode($email) . "&login_password=" . urlencode($password);

    foreach($whattosend as $key=>$value){
        $stringtosend = $stringtosend . "&" . $key . "=" . urlencode($value);
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$URL);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $stringtosend);
    $storage = curl_exec ($ch);
    curl_close ($ch);
    return $storage;
}
$leaddata = array();
$leaddata['create'] = "lead";
$leaddata['name'] = trim($_POST['name']);
$leaddata['phone'] = trim($_POST['phone']);
$leaddata['email'] = trim($_POST['email']);
$leaddata['moreinfo'] = trim($_POST['moreinfo']);

$leaddata['category'] = "Source: Website\nUnprocessed";

$whathappened = SendtoHeap ($leaddata);

if(strtolower(trim($whathappened))=="success"){
    $whathappened = true;
}else{
    $whathappened = false;
}
?>

Following Up

Ok, now assuming creating the lead worked, let’s create two events to followup with.

Code:

<?php
function SendtoHeap ($whattosend){
    $URL = "https://wbp.crmondemand.biz/api.php";
    $email = "ben@wbpsystems.com";
    $password = "password";
    $stringtosend = "login_email=" . urlencode($email) . "&login_password=" . urlencode($password);

    foreach($whattosend as $key=>$value){
        $stringtosend = $stringtosend . "&" . $key . "=" . urlencode($value);
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$URL);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $stringtosend);
    $storage = curl_exec ($ch);
    curl_close ($ch);
    return $storage;
}
$leaddata = array();
$leaddata['create'] = "lead";
$leaddata['name'] = trim($_POST['name']);
$leaddata['phone'] = trim($_POST['phone']);
$leaddata['email'] = trim($_POST['email']);
$leaddata['moreinfo'] = trim($_POST['moreinfo']);

$leaddata['category'] = "Source: Website\nUnprocessed";

$whathappened = SendtoHeap ($leaddata);

if(strtolower(trim($whathappened))=="success"){
    $whathappened = true;
}else{
    $whathappened = false;
}

if($whathappened){
    $nextweek = time()+(7 * 24 * 60 * 60);
    $twoweeks = time()+(14 * 24 * 60 * 60);

    $eventdata = array();
    $eventdata['create'] = "event";
    $eventdata['year'] = date("Y",$nextweek);
    $eventdata['month'] = date("n",$nextweek);
    $eventdata['day'] = date("j",$nextweek);
    $eventdata['title'] = "First Call";
    $eventdata['calendar'] = "Sales Tasks";
    $eventdata['association'] = $leaddata['name'];

    $Sf = SendtoHeap ($eventdata);

    $eventdata = array();
    $eventdata['create'] = "event";
    $eventdata['year'] = date("Y",$twoweeks);
    $eventdata['month'] = date("n",$twoweeks);
    $eventdata['day'] = date("j",$twoweeks);
    $eventdata['title'] = "First E-Mail";
    $eventdata['calendar'] = "Sales Tasks";
    $eventdata['association'] = $leaddata['name'];
    $eventdata['emailtemplate'] = "Thank you for your interest";

    $Sf =SendtoHeap ($eventdata);
}
?>

Again, lots of stuff going on here. We start out by establishing two variables containing a timestamp for one and two weeks from now. Then we use those to setup the variables for the two events. The first event is a call that you must complete, the second is an event that sends out the email template “Thank you for your interest.”