This week I’ve created a screencast in addition to the normal write-up. It’s my first time screencasting, so please be gentle!
If you want to do really powerful things with Infusionsoft then you’ll need to use the API. Infusionsoft provides a PHP SDK that is used by a lot of developers. It’s free, powerful, and has a fair amount of documentation. Unfortunately it also has a few issues that developers frequently encounter (such as random blank responses from the API or error messages), and has a bit of a learning curve.
At Novak Solutions we’ve developed a new and improved Infusionsoft PHP SDK. It is completely free, and we use it for all of our products so you know it’ll be maintained in the future. It has a few features that we feel make it easy to use and better than the official SDK:
- No dependencies. If your server has PHP and cURL, then you are good to go!
- Code completion. We’ve added all the necessary PHPDoc comments so code completion will work in popular editors, like Eclipse and PhpStorm.
- Automatically retries. Automatically retries failed API calls when it is safe to do so (i.e., updates and deletes, but not inserts).
- Automatically handles XML-RPC. Your requests and responses are automatically encoded and decoded. We’ve included a custom version of the XML-RPC code that automatically works around a few bugs in the Infusionsoft API.
- Automatically picks the right method. Saving records is easier. The SDK will automatically pick whether to do an insert or an update based on whether you are working with a new or existing record.
- Future safe! The SDK will keep working, even if Infusionsoft removes a field in the future.
- Does everything the official SDK does. If the official SDK can do it, then the Novak Solutions SDK can do it, too.
We think our SDK is easier to use than Infusionsoft’s, but you’ll still need to know PHP in order to use it. If you aren’t a coder but want to use the API to do your own integrations, check out our Easy API product.
To help you get started I’d like to walk through a basic example of how to add a contact to your Infusionsoft app using the SDK and the API.
The first step is to download the SDK. You can download a zip file of the SDK, or you can use git to clone our entire repository:
git clone email@example.com:joeynovak/infusionsoft-php-sdk.git
You can unzip or clone the SDK just about anywhere you want. For security, it is best to put it somewhere that isn’t accessible from a URL.
The SDK comes with an example configuration file. Copy Infusionsoft/config.sample.php to Infusionsoft/config.php. Edit this file and add your Infusionsoft URL and API key. If you need help finding your API key, check out this YouTube video.
Next you’ll need some code. This is a complete example of how to add a contact to an Infusionsoft app:
<?php require_once('Infusionsoft/infusionsoft.php'); $contact = new Infusionsoft_Contact(); $contact->FirstName = 'Jacob'; $contact->LastName = 'Allred'; $contact->Email = 'firstname.lastname@example.org'; $contact->save();
If you save that snippet as example.php in your SDK folder and run it, then my name and email address should get added to your Infusionsoft app.
The require_once line includes the SDK into your script. You only have to include this single file. Everything else will automatically get loaded as needed.
The next line creates a new Infusionsoft_Contact object. You can create new objects for any Infusionsoft table that you might need. A full list of Infusionsoft tables and fields is available at the Table Documentation page. If you were wanting to update a contact instead of creating a new one, you could pass in the contact ID like this: $contact = new Infusionsoft_Contact(1234);.
The next 3 lines set the fields we’d like to insert or update. The field names match the table field names found in the Table Documentation.
The last line saves the contact to your Infusionsoft app. The SDK automatically figures out if it needs to use the update or insert API call. This keeps your code clean and helps prevent mistakes.
You can also use custom fields, but you’ll need a few extra lines of code to let the SDK know that the custom fields exist. First you’ll need to find your custom field’s name. Go to the custom fields settings page in your Infusionsoft app, then click View the field database names at the top of the page. You’ll need to add an underscore to the front of your custom field’s database name. For example, if I had a custom field named Favorite Food, then my code would look like this:
<?php require_once('Infusionsoft/infusionsoft.php'); $contact = new Infusionsoft_Contact(); $customFields = array( '_FavoriteFood', ); $contact->addCustomFields($customFields); $contact->FirstName = 'Jacob'; $contact->LastName = 'Allred'; $contact->Email = 'email@example.com'; $contact->_FavoriteFood = 'Pizza'; $contact->save();
We’ve been using this SDK for several years now. We’ve found it helps us create more reliable integrations for our customers. It also allows us to get projects done faster than we could with the official SDK.
Have a question about the SDK? Have an idea for a tip, or a project you’d like us to help you with? Let us know!