Save in Custom Values

This article assumes you have read the previous article titled “Create Lead and Follow-up Events”.

All custom values are placed in a single post variable (entitled “custom”) in the form of a CSV. This CSV has the first column with custom value names and second column with values. That’s sort of confusing, so let’s try to visualize that. Let’s say this CSV was a spreadsheet, it might look like this:

Ok, first I’m going to create a function to write CSVs (this also includes the code from the previous article):

Code:

<?php
function WriteCSVforHeap($dataarray){
    $tempfile = tmpfile();

    foreach($dataarray as $key => $value){
        fputcsv($tempfile, array($key,$value));
    }

    fseek($tempfile,0);
    $storage = stream_get_contents($tempfile);

    fclose($tempfile);
    return $storage;
}

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);
}
?>

Ok, what’s going on here. Well, I’m taking an array of values and changing the keys to be the first column and the value to be the second column. Then I’m outputting the whole CSV as a result.

One might ask (reasonably) why I’m using PHP’s build in CSV functions. After all a CSV is just commas and newlines. The answer is about escaping and encoding. You control the names of custom values, not what people may be posting into them (because you are retrieving via post).

Ok now let’s use the function:

Code:

<?php
function WriteCSVforHeap($dataarray){
    $tempfile = tmpfile();

    foreach($dataarray as $key => $value){
        fputcsv($tempfile, array($key,$value));
    }

    fseek($tempfile,0);
    $storage = stream_get_contents($tempfile);

    fclose($tempfile);
    return $storage;
}

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";

$csvarray = array();

$csvarray['Refererral'] = $_POST['referral'];
$csvarray['Assistant'] = $_POST['assistant'];
$csvarray['Segment'] = $_POST['segment'];

$leaddata['custom'] = WriteCSVforHeap($csvarray);

$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);
}
?>