Have you ever had a situation where an external web form, maybe on a referral partner’s site, was sending you names as a single field? The challenge is that you need to get them into Infusionsoft as Title, First Middle Last, Suffix and maybe even nickname.
How do you do that?
You can write a simple little script that looks something like this (from stackoverflow.com):
|1||$name_parts = explode(‘ ‘, $name);||2||$first_name = $name_parts;||3||$last_name = $name_parts[sizeof($name_parts)-1];|
But if you really want to do the job properly, you will want to use a public domain project, by Jason Priem, called the HumanNameParser. Here is how you set it up and add the results to a contact record in Infusionsoft:
First, go to GitHub and download the latest version of the project, like so:
When you unzip the file, you will have a folder, called HumanNameParser.php-master. Using your FTP client, upload then entire folder to your scripts folder on your website folder. (Note: your scripts folder is the one where you have installed the iSDK.)
Next, we will create a script in the scripts folder, called parse-names.php. Here is the full script:
Lines 3 – 7 create the connection between your app and Infusionsoft. If you have any questions about this section, I recommend my previous post, #5 – How I start my Infusionsoft project code.
Line 11 starts with require_once, which includes the content of a PHP file into another PHP file before the server executes it. So, in this case, we are saying we need to include the contents of init.php, which is in the HumanNameParser folder, in order to execute this script.
NOTE: If the HumanNameParser folder is not in the same directory as your script, you will have to edit the path of the init.php file in the parameter of require_once.
// Get the contact ID $name = 'John D. Rockefeller'; $email = 'firstname.lastname@example.org'; // $name = $_POST['FullName']; // $email = $_POST['Email'];
Lines 13 – 18 assign the information being passed from your form, in this case, name and email address, to a variable, so we can work with it.
Notice I have two sets of the same two variables: $name and $email. When writing scripts, I always start out by assigning a fixed value first. Then, when I have everything working properly. I comment out the hard-coded information and use the information being passed from the form or http post.
So, in this case, lines 15 and 16 have the hard coded values John D. Rockefeller and John D’s email address. This is my dummy data. It makes it easy to test your script in a browser.
When I am ready to deploy the script, the code will look like this:
// Get the contact ID // $name = 'John D. Rockefeller'; // $email = 'email@example.com'; $name = $_POST['FullName']; $email = $_POST['Email'];
Now the dummy data is commented out and the $_POST variables are being assigned to those variables.
So what is a $_POST variable? Well, if you look at the html of a form, you will see the form tag looks something like this:
<!-- this tells the browser where and how to send the data from the form --> <form action="CloneOpp.php" enctype="multipart/form-data" method="post">
After the form tag itself is action=”something”. This tells it where to send the information collected on the form. In this example, it is sending to a script called CloneOpp.php. For our name parsing script, however, we would want to send it to parse-names.php, like so:
<!-- this tells the browser where and how to send the data from the form --> </form><form action="parse-names.php" enctype="multipart/form-data" method="post">
Next is the method. What does it say? POST! The information is being sent using HTTP POST. So our form is putting the information in a superglobal called $_POST.
Notice the $ in front of $_POST. This tells you it is a variable. A variable is just a tupperware container that you put information in, like leftovers, so you can store it, move it around and use it elsewhere in your script. $_POST is just a special kind of variable, a kind of super variable, used to collect form data.
Let’s look at the entire form this time:
Web Form Test <!-- this tells the browser where and how to send the data from the form --> </form><form action="parse-names.php" enctype="multipart/form-data" method="post">Name : <input name="FullName" type="text" /></form><form action="parse-names.php" enctype="multipart/form-data" method="post">Email : <input name="Email" type="text" /> <input type="submit" value="Submit" /> </form><form action="CloneOpp.php" enctype="multipart/form-data" method="post"> <pre>
Notice the two text fields. Each have a name. The first one is ‘FullName’ and the second is ‘Email’. NOTE: look at name= in the <input> tag, not the field label out to the left.
So when the form passes this information to the script as an HTTP POST, the information will be in the superglobal $_POST in the form of $_POST[‘name’]. Hence, our script assigns the value of $_POST[‘FullName’] to a tupperware container called $name and $_POST[‘Email’] to a variable $email.
Let’s look at it again so you can review:
$name = $_POST['FullName']; $email = $_POST['Email'];
Now, we can take that information, now safely stored in our tupperware, and do something with it.
What do we want to do? Well, the first thing we want to do is parse the name into its various parts. Then we want to write that information to the corresponding fields in the Infusionsoft contact record.
So let’s do some parsing. This is where the HumanNameParser comes in.
$parser = new HumanNameParser_Parser($name);
First, we are going to use this line of code to create a new parser object that takes $name in the argument (that is the part in parentheses) and spits whatever is in $name back out in pieces. Remember, we assigned the name from the form to the variable $name. Now we are feeding it into the parser.
// Use the methods to get First, Middle and Last // Available methods are getleadingInit(), getFirst(), getNicknames(), // getMiddle(), getLast() and getSuffix() $fname = $parser->getFirst(); $mname = $parser->getMiddle(); $lname = $parser->getLast();
Line 26 uses the getFirst method to … you guessed it, get First Name from the full name submitted. You don’t have to worry about the code to do that. You just have to know the name of the method that does it.
Similarly, line 27 gets middle name and line 28 gets last. Available methods are listed in the comments on lines 24 and 25.
Just as a side note, the HumanNameParser handles comma-reversed names (‘Smith, John’), names with non-English symbols, names with odd capitalization or punctuation (‘e e cummings’), first names made of initials (‘J.K. Rowling’), etc.
OK. So now we have the different name parts split out. It is time to put them into Infusionsoft. For that, we are going to use the addWithDupCheck method.
$data = array('FirstName' => $fname, 'MiddleName' => $mname, 'LastName' => $lname, 'Email' => $email);
Lines 31 – 34 create an array that include the information we want to add to the Contact record. You can read more about arrays and all the different possible formats here. But when we are creating an array of data to load into an Infusionsoft record, it is almost always going to have a series of comma separated key => value pairs.
So, in this example, we are creating a variable called $data and we are telling PHP this is an array. Inside that array, we are saying the Contact field called FirstName should get the information stored in $fname. The Contact field MiddleName gets $mname, etc. See the table documentation for the Contact table to review the various field names.
Finally, we are going to call the API method, addWithDupCheck, on line 35. addWithDupCheck does just what it sounds like. It is going to first check if a contact exists that matches this data. If not, it will Add a new contact. If so, it will update the existing contact.
$update = $app->addWithDupCheck($data, 'EmailAndName');
addWithDupCheck takes two arguments. (Those are in parentheses.) The first is the information we want to add or update. In this case, we have already put that in an array assigned to $data, so we just have to put $data for the argument.
The second is the method of checking for duplicates. The options are: ‘Email’, ‘EmailAndName’, ‘EmailAndNameAndCompany’. You can see, on line 35, we are using EmailAndName.
Last but not least, we put the api call in a variable called $update. When an Add or Update API call is successful, it will return the ContactId of the record added or updated. So just to check that everything went OK, we echo $update on line 37.
Get the files – (NOTE: Double-click the download file to unpack contents)
Whew! So what questions do you have? Feel free to post them in the comments.