Tuesday’s Tip: An easier Infusionsoft PHP SDK

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:

  1. No dependencies. If your server has PHP and cURL, then you are good to go!
  2. Code completion. We’ve added all the necessary PHPDoc comments so code completion will work in popular editors, like Eclipse and PhpStorm.
  3. Automatically retries. Automatically retries failed API calls when it is safe to do so (i.e., updates and deletes, but not inserts).
  4. 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.
  5. 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.
  6. Future safe! The SDK will keep working, even if Infusionsoft removes a field in the future.
  7. 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 git@github.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 = 'jacob@novaksolutions.com';
$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 = 'jacob@novaksolutions.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!

24 Comments

  1. I have the SDK installed and the example above working properly. I’d like to utlize the ContactAction object and getTable() method, but I also want to pass a specific customer ID so it only returns values for that customer. Can I do that? Can you help me with the syntax?

    Reply
    • You’ll need to use the DataService::query(). Something like this should work:

      $ContactId = 123; // The contact ID you are searching for.
      $ContactActions = Infusionsoft_DataService::query( new Infusionsoft_ContactAction(), array('ContactId' => $ContactId) );

      That would populate $ContactActions with an array of Infusionsoft_ContactAction objects for the contact specified in $ContactId.

      Hope this helps!

      Reply
  2. Thanks Jacob. I’ll give it a try. Another API related question – can you clarify what is considered a single call for Infusionsoft API throttling? I know it’s 10,000 tokens, 1 call equals 1 token, you gain back 2 tokens per second of down time. But what is a considered a single call? When I get the query working properly and hit that CustomerAction table to download all records for a single contact – how do I determine how many “calls” that’s actually going to use up? I’m only sending the request one time, but there could be 150 pieces of data coming back.

    Another example – the exporter tool contained in the Novak SDK. When I choose the table and click submit, how can I know how many calls that’s going to use up?

    thanks
    Nick

    Reply
    • Each call is 1 token, so the example I gave you above would take 1 token regardless of how many records are returned. Infusionsoft has announced they are modifying this system so you can request higher API limits when needed, but we prefer to make efficient apps that stay within the original limits as much as possible.

      For the exporter example, there isn’t an easy way to know how many API calls it will take. For example, if you are exported 1500 records, it’d take at least 16 calls: 1 call for each 100 records (a “page”), and an additional call to get back an empty page with no records so the code knows it has gotten all the records.

      You could potentially save a call there by having it check to see if the number of returned records is less than the requested records per page, thus knowing that you’ve gotten all the records.

      Reply
  3. Jacob, I’m using the code you gave above and it is pulling in the correct table, but I can’t get it to pull any values. It continues to pull no rows regardless of the id I’m using. Do you guys offer consultations? Can I hire for 30 minutes to help me get this in working order?

    Reply
    • Nick, you can email me at jacob@novaksolutions.com if you’d like to discuss a paid consultation.

      You could try using the following to search for all records in ContactAction, so you can see what a valid return would look like:

      $ContactActions = Infusionsoft_DataService::query( new Infusionsoft_ContactAction(), array('Id' => '%') );

      Reply
  4. Hi Jacob, Once I use this API and insert a contact, how can I return the new ID of the contact to be able to add a tag to them? Thanks

    Reply
    • The ID will be populated in the object. So if you do $contact->save(), then $contact->Id would contain the Id of the new contact.

      Reply
  5. Hi, While creating new contact , How can I append some affiliate id into it ?

    Reply
    • Hmm I’ve never done this before. The affiliate ID isn’t stored with the contact. I suspect you need to add an entry in the Referral table after you create the contact and before you create an order or subscription (see: https://developer.infusionsoft.com/docs/read/Table_Documentation). This would link the affiliate with the contact.

      Reply
      • Hi,

        yes I have used dsAdd function to add entry for adding AffiliateId, But in Referral Partner Center(/Affiliate/tools/linkstats.jsp) it does show only Hits, not Opt- ins.

        Where as contact gets created and in backend I can see referral details too.

        I double checked for email being opted in, It is opt it but in Partner Center it shows No opt-ins

      • Sorry, I don’t have any experience with affiliates via the API. Also, we don’t use the Infusionsoft SDK at all, so I have no idea how to do anything with that SDK.

      • Thanks for your time 🙂

  6. How to assign campaign ID while adding contact, Thanx in Advance

    Reply
    • I’m not sure I understand the question. If you are asking how to put a contact into a campaign, the easiest method is probably to assign a tag that your campaign is expecting.

      Reply
  7. I have the SDK installed and everything was working properly, with the exception of the custom fields. I’m not trying to save any data, all I’m trying to do is pull a specific custom field’s data.

    Reply
    • Eddie, just to be sure, are you using an underscore at the beginning of your custom field name? Custom fields are the only ones that require this.

      Reply
  8. Hi!

    I’m getting this error while running the example file. I’ve created the config.php file with proper infusionsoft app name and valid key. Can you please help?

    Fatal error: Uncaught CURL error: error:0D0C50A1:asn1 encoding routines:ASN1_item_verify:unknown message digest algorithm Attempted: 1 time(s). thrown in C:\wamp\www\infusionsoft_test\Infusionsoft\App.php on line 101

    Reply
    • This error indicates that your client (e.g., your server) is running dangerously out-of-date software. You’ll need to update OpenSSL on your server before you’ll be able to run the example.

      Reply
  9. Thanks for the tutorial & the SDK. How would you use this to add a tag to the contact?

    Reply
  10. This looks really good – two quick questions:
    1. Any chance you wanted to add this to packagist.org so it could be installed using composer?
    2. Which Infusionsoft API did you build this on top of? The iSDK or the PHP SDK?

    Thanks a ton!!
    greg

    Reply
  11. My mistake! You’re already on packagist.org!

    So I installed the SDK using composer but it’s not going so well so far. I tried using the OAuth code that’s on the packagist page but get errors that Infusionsot_OAuth2 can’t be found. I tried using the contact code that’s at the top of this page, but I get complaints about not being able to find Infusionsoft/infusionsoft.php if I include the require() line or Infusionsoft_Contact if I don’t.

    Isn’t the point of composer that it’s supposed to handle all of these autoloading issues?

    I’d love any suggestions on what to try at this point!

    Thanks,
    greg

    Reply
  12. Hi
    Enjoying using your API but having some issues with Find By Email.

    I am trying to look up an email to get the ID but I can not seem to work it out with your SDK.

    Any help would be appreciated.

    Malcolm

    Reply
  13. Thank you so much……

    Reply

Leave a Reply