home

A QuickBooks PHP SDK / API example with OAuth THAT ACTUALLY WORKS as of 2015 / January

"Hello World" is Non-Trivial

Pearce at Intuit (maker of QuickBooks) thought a better example was necessary--better than the 8MB of sample PHP code on Intuit's site. His example is great in that it's tiny--the minimum needed for "hello world." Unfortunately, his example doesn't work, either, or at least it doesn't anymore. It's still a great example overall, though, because it's small enough that I could debug it and get it working.

Working code, but...

Below is my truly working code. However, both my code and Pearce's is only for example purposes. Amongst other issues, there are security issues with it. I left Pearce's ReadMe and comments largely intact; he talks to some extent about what you'd need in production.

Ingredients

Two sets of ingredients:

First, various Intuit account / security stuff:

  1. An Intuit developer account (free)
  2. Within your account, you need to create an app and then its APP Token, OAuth Consumer Key, and OAuth Consumer Secret. (Getting those is described well on Intuit's site, as I recall, now that you know what to search for.)
  3. You need a QuickBooks Online sandbox account. (I don't remember exactly how I got that.)

Second, code stuff:

  1. My ZIP file with 5 files: config.php, index.php, oauth.php, ReadMe.txt, and license.txt
  2. The PHP SDK for QuickBooks
  3. You need to make a number of changes in the config.php from my ZIP file: see below.
  4. If you're using the QuickBooks Online sandbox, you'll need to make two small changes in the SDK: see below.
  5. You'll need a LAMP (LAP) / WAMP (WAP) environment or something equivalent. (You do not need MySQL.)
  6. You'll need the official PHP OAuth library installed. See Ubuntu instructions below.

Changes to config.php

  1. if needed, change the session_save_path. The web server must have permission to write to that folder. (NOTE: /tmp/sam2 is probably not secure. This is supposed to be a sandbox-only simple example to start from)
  2. Enter your OAuth Consumer Key and Secret
  3. Enter your PATH_SDK_ROOT

Changing the PHP SDK sdk.config to sandbox mode

In the SDK root, change sdk.confg so that baseURL looks like the following; the CDATA stuff--begin and end tag--are for XHTML compliance. Do NOT add that to the sdk.config file. You're adding exactly 16 non-whitespace characters.

Installing PHP OAuth in Ubuntu

# apt-get update
# apt-get install libpcre3 libpcre3-dev
# pecl install oauth
THEN, add the line

extension=oauth.so
to
/etc/php5/apache2/php.ini

Restart apache after modifying php.ini.

The above is said to have worked in Ubuntu 12.04. I did it in Ubuntu 14.04 LTS.

Adding Customers

Below this code in index.php:

Add this:

if (0) { // turn off if dup
$customer = new IPPCustomer();
$customer->DisplayName = "Eleven";
$dataService->add($customer);
}

You have to turn 0 to 1. Then, after listing 10 customers, the code will add customer "Eleven." I would guess you can add "11," but I did not care to find out at that point.

Note that if you try to add a duplicate customer, you'll get

I do not know yet how to properly handle the duplicate situation.

Everything that Can Go Wrong / From Pearce's version to mine

Here's all the things that wrong if you try to run Pearce's code by itself.

Warning: session_start(): open(/var/www/temp/sess_..., O_RDWR) failed: No such file or directory (2) in config.php on line 7

Solution: set your session_save_path('/var/www/temp'); to a directory that exists and that Apache has access to (and, eventually, is secure)

If you see a Session warning above you need to run the command: 'chmod 777 temp' in the terminal on the code page.

Solution: Well, more specifically, you need to set 777 permission for whatever you set session_save_path() to.

Set the consumer key and secret in the config.php file before you run this example

Solution: In config.php, as described above.

Warning: require_once(../v3-php-sdk-2.0.1/config.php): failed to open stream: No such file or directory in index.php on line 29

Solution:

  1. define and properly set PATH_SDK_ROOT in config.php. Set the right version / folder name.
  2. add require_once(PATH_SDK_ROOT . 'config.php'); in config.php
  3. comment out require_once('../v3-php-sdk-2.0.1/config.php'); in index.php

Warning: Cannot modify header information - headers already sent by (output started at config.php:9) in oauth.php on line 27

Comment out echo "<h4>If you see a Session warning [...] in config.php

GET https://appcenter.intuit.com/trackapi/TrackingActions [...] results in a 403 Forbidden error in the Chrome Developer Tools Network console (or equivalent).

Replace that URL, in index.php, with
https://appcenter.intuit.com/Content/IA/intuit.ipp.anywhere-1.3.1.js

PHP SDK version

I started with the file v3-php-sdk-2.0.5.zip. (Note that I made changes as described above.)

Pearce's Code

QuickBooks OAuth + List Customers - Example [PHP, Intuit, Intuit Partner Platform and IPP]

http://code.runnable.com/UtEZ4OpSEoFzAACs/quickbooks-oauth-list-customers-example-for-php-intuit-intuit-partner-platform-and-ipp

That was posted roughly in early 2014--"a year ago" as of 2015/Jan.

License

Please copy the code and documentation (including this page's content). Give me credit and / or blame. Also note that the code I started from is under the MIT License, as stated in the license.txt file.

Page History

Other keywords: QuickBooks API

HTML5 valid