In the last Extending Infusionsoft webinar, I mentioned that I have a boilerplate of code that I use every time I start a new php file for a PHP project.
In the post-webinar survey, many of you mentioned that you would like a copy of that boilerplate. Ask and you shall receive …
Here is copy of the code and a line-by-line explanation so you can use it as the starting point for your own project scripts …
<?php echo "Hello World! <br/>"; // Connect to Infusionsoft // This is the "loud" way require_once("isdk.php"); $app = new iSDK; if ($app->cfgCon("connectionName")) { echo "Connected..."; }else { echo "Not Connected..."; } // Connect to Infusionsoft the "quiet" way // require_once("isdk.php"); // $app = new iSDK; // $app->cfgCon("connectionName"); // Current date in Infusionsoft-friendly format echo $currentDate = date("YmdTH:i:s") . "<br/>";
So let’s start at the very most basic level … to designate what follows as PHP, we use the following …
<?php
The next line is the cliché line …
echo "Hello World! <br/>";
This line prints the phrase “Hello World!” on your screen. This comes before the connection to Infusionsoft and just tells me that I have reached a functioning PHP page. Notice that because I am working in PHP, I have to place the HTML I want rendered (Hello World! <br/>) in quotes and end with a semi-colon.
Next comes our connection to Infusionsoft. Notice that lines 7 – 17 are what I call the “loud way.” Lines 20 – 24 are the “quiet way.”
What I mean by the loud way is that it has ECHO statements in it to reassure me the connection to Infusionsoft is working correctly. When putting the final touches on your code, you may want to replace the loud way with the quiet way. Both make the same connection.
Let’s go through each line of code so you understand what it does …
require_once("isdk.php");
This line says, to be able to execute the script in this file, we need to access an external file called isdk.php. As written, this assumes isdk.php is in the same folder as your script. If it isn’t, you will need to change this to the appropriate path.
$app = new iSDK;
This line is one of those things that until you really get into PHP, you just need to do without really understanding. The short version is that iSDK is a class and by saying “new iSDK” we instantiate that class and then assign it to a variable named $app.
See? I told you … don’t worry about it. Just know it has to be there.
But while we are on the subject, I do want to say a word about the variable $app. We can name this variable anything we want … $pony, $dog, $pie, $wheeee … it doesn’t matter. What does matter is that once you name it something, it has to be consistent.
I name it $app for the very simple reason that that is what it is in all the Infusionsoft documentation. That way, when I copy and paste a code sample from the documentation, I don’t accidentally end up with an error because my variables don’t match.
if ($app->cfgCon("connectionName")) { echo "Connected..."; }else { echo "Not Connected..."; }
This is an IF statement. It takes the form of IF this is true, do this, ELSE do this other thing. “This” is between the first set of brackets. “This other thing” is between the second set of brackets.
So, let’s look at the statement that needs to evaluate to TRUE …
$app->cfgCon("connectionName")
This is what we call an API call. It is the first one in a script and it establishes a new connection between your script and Infusionsoft application using the information provided in a different file called conn.cfg.php.
If it connects successfully, the variable $app will evaluate to TRUE because cfgCon has a data type of boolean, meaning it returns either TRUE or FALSE.
From the API documentation:
connectionName - this is the connection name from your conn.cfg.php file //Below is what the entry your conn.cfg.php file looks like: $connInfo = array('connectionName:applicationName:i:insertYourApiKeyHere: You can place a note here');
If we look at my conn.cfg file, you will see that you can put more than one application in your config file and then just call the correct one using the connection name.
In my case, I just left the connection name for the app, hff89622, as connectionName. That is why on line 12, it says connectionName. However, if I wanted to connect to dfy90994 instead, line 12 would read like this:
$app->cfgCon("sandbox")
As I said before, the “quiet way” just removes the IF Statement and is commented out in the version above.
echo $currentDate = date("YmdTH:i:s") . "<br/>";
The last thing I put in my boilerplate is an Infusionsoft-friendly timestamp. The core of this statement uses the PHP date() function which formats a timestamp according to the parameters it is given.
In this case, the format parameters are YmdTH:i:s, which is the format required by Infusionsoft. The rest of the statement assigns the result to a variable, $currentDate, so it can be ECHOed and appends an HTML line break at the end so that whatever comes after the date will be on the next line.
You may wonder why I include this DateTimestamp in my boilerplate? There are a couple of reasons: 1) For whatever reason, I work with dates A LOT! This way I don’t have to keep looking up the Infusionsoft format; and 2) when I am testing my scripts, I usually call them in a browser first.
Given my “dangit” method of writing code, I do a lot of changing then refreshing, changing then refreshing. Having that date/time there reassures me the page is reloading even when nothing else changes. You can see an example of the format and time changing with each refresh below.