PHP

Tutorial Objective

The QuickBooks Online PHP SDK makes it easy to integrate your PHP web app with the QuickBooks Online API. This guide assumes that you have an existing web app that you want to integrate with QuickBooks Online.

This tutorial includes the following:


1
Install QuickBooks PHP SDK via Composer
2
Create an App on the Intuit Developer portal
3
Connect to QuickBooks Online
4
Make QBO API request
Prerequisites

To follow this tutorial,You need the following installed on your machine:

  • PHP 5.6 or greater
  • To use the PHP Guzzle handler, install guzzlehttp/guzzle via Composer
  • To use the cURL handler, you must have cURL version 7.19.7 or greater compiled with OpenSSL
  1. Install QuickBooks PHP SDK via Composer

Note

Note

This step can be skipped for running the Hello, World! Sample app.

The recommended way to install the QuickBooks PHP SDK is with Composer. Composer is a dependency management tool for PHP that allows you to declare the dependencies your project needs and installs them into your project.

The installation of composer is local by default.

Install Composer ( if not installed )

1
 curl -sS https://getcomposer.org/installer | php

You can add the SDK as a dependency using the composer.phar CLI:

1
 composer require quickbooks/v3-php-sdk
Alternatively ( If specifying SDK as a dependency in composer.json ) :

1
2
3
4
5
{
    "require": {
        "quickbooks/v3-php-sdk": ">=*"
    }
}
After installing, require Composer’s autoloader:

1
2
3
<?php
require 'vendor/autoload.php';
?>
For more on how to install Composer, configure autoloading, and other best practices for defining dependencies, go to getcomposer.org.
  1. Create an app on the Intuit Developer portal

Note

Note

Skip to section 3 if you have already created an app in the developer portal.

To connect your Hello, World! app to a QuickBooks company, you need to create an app on the Developer Portal .


1
Sign into the Developer Portal and click My Apps.
2

Click Create new app. On the Create new app page, click Select APIs under Just start coding.

View Screenshot

qbo/docs/develop/sdks-and-samples-collections/AppCreation_PHP.png
3

Select Accounting and then click Create app as shown below.

View Screenshot

qbo/docs/develop/sdks-and-samples-collections/AppCreation_Type_PHP.png

Note

Note

Creating a developer account also creates a QuickBooks test company (also referred to as Sandbox company). You are entitled to create at the most 5 sandbox companies.

You will use this Sandbox company to go through the authorization flow in the subsequent steps to see what it looks like.
  1. Connect to QuickBooks Online

To get access to the data of a QuickBooks company, a user must authorize your app through an authorization flow. At the end of the authorization flow, an Access Token is generated, which is used to make QuickBooks Online API requests. To initiate the authorization flow, users of your app will click on the Connect to QuickBooks button. The steps in this section go over how to build this Connect to QuickBooks button.


Creating a developer account also creates a QuickBooks test company (also referred to as Sandbox company). You will use this Sandbox company to go through the authorization flow to see what it looks like.


1

This authorization flow requires app’s Client Id and Client Secret from your app’s Keys tab.

View Screenshot

qbo/docs/develop/sdks-and-samples-collections/AppKeys_PHP.png
2

During the Authorization flow, your app is redirected to an Intuit server to get the Authorization Code after validating the user’s username and password. This Authorization code is sent to the Redirect URL you specify in your app’s Keys tab as shown. For the sample app that is used in this document , the Redirect URL is http://localhost:8080/callback.php

View Screenshot

qbo/docs/develop/sdks-and-samples-collections/AppRedirectURI_PHP.png
3

Configure Client Id and Client Secret along with Redirect URL in your code’s config.php like shown here. View the config-sample.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php
return array(
    'authorizationRequestUrl' => 'https://appcenter.intuit.com/connect/oauth2',
    'tokenEndPointUrl' => 'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer',
    'client_id' => 'Enter the clietID from Developer Portal',
    'client_secret' => 'Enter the clientSecret from Developer Portal',
    'oauth_scope' => 'com.intuit.quickbooks.accounting openid profile email phone address',
    'oauth_redirect_uri' => 'http://localhost:8080/callback.php',
)
?>
4

To initiate the Authorization Flow, specify the scope, Client Id, Redirect Url to redirect user to AuthorizeEndpoint referred before. Snippet of code is shown here, full code can be referred in github file index.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<?php
$dataService = DataService::Configure(array(
    'auth_mode' => 'oauth2',
    'ClientID' => $config['client_id'],
    'ClientSecret' =>  $config['client_secret'],
    'RedirectURI' => $config['oauth_redirect_uri'],
    'scope' => $config['oauth_scope'],
    'baseUrl' => "development"
));

$OAuth2LoginHelper = $dataService->getOAuth2LoginHelper();

// Get the Authorization URL from the SDK
$authUrl = $OAuth2LoginHelper->getAuthorizationCodeURL();
?>

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php
return array(
    'authorizationRequestUrl' => 'https://appcenter.intuit.com/connect/oauth2',
    'tokenEndPointUrl' => 'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer',
    'client_id' => 'Enter the clietID from Developer Portal',
    'client_secret' => 'Enter the clientSecret from Developer Portal',
    'oauth_scope' => 'com.intuit.quickbooks.accounting openid profile email phone address',
    'oauth_redirect_uri' => 'http://localhost:8080/callback.php',
)
?>
5

Add a button Connect to QuickBooks which would initiate the Authorization Flow. You can see how this is implemented in the sample code here ( index.php )

View Screenshot

qbo/docs/develop/sdks-and-samples-collections/AppC2QB_PHP.png
6

Click on Connect to Quickbooks button you just embedded into your app above and and the user goes through the authorization flow on this AuthorizeEndpoint which looks like:

View Screenshot

qbo/docs/develop/sdks-and-samples-collections/AppAuthorize_PHP.png

After user clicks on the Connect button, the request is sent to an Intuit server. When request is successful is processed, Intuit server responds with an authorization code and QuickBooks Company ID (also called Realm ID) on the Redirect URL, which is handled here ( callback.php )

1
$parseUrl = parseAuthRedirectUrl($_SERVER['QUERY_STRING']);
7

This authorization code is exchanged for Access/Refresh Token using the TokenEndpoint. We set the Tokens into a session variable. Access tokens are used in an API request and Refresh tokens are used to get fresh short-lived Access tokens after they expire. you can look at the code here


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
function processCode()
{

    // Create SDK instance
    $config = include('config.php');

    $dataService = DataService::Configure(array(
        'auth_mode' => 'oauth2',
        'ClientID' => $config['client_id'],
        'ClientSecret' =>  $config['client_secret'],
        'RedirectURI' => $config['oauth_redirect_uri'],
        'scope' => $config['oauth_scope'],
        'baseUrl' => "development"
    ));

    $OAuth2LoginHelper = $dataService->getOAuth2LoginHelper();
    $parseUrl = parseAuthRedirectUrl($_SERVER['QUERY_STRING']);

    /*
     * Update the OAuth2Token
     */
    $accessToken =    $OAuth2LoginHelper->exchangeAuthorizationCodeForToken($parseUrl['code'], $parseUrl['realmId']);
    $dataService->updateOAuth2Token($accessToken);

    /*
     * Setting the accessToken for session variable
     */
    $_SESSION['sessionAccessToken'] = $accessToken;
}
?>
  1. Make QBO API Request

Once the tokens are received, tokens can be used to make QuickBooks Online API calls. As you can notice from the sample, we have a script to make API Call, ( apiCall.php) refer screenshot below:

View Screenshot

qbo/docs/develop/sdks-and-samples-collections/AppMakeAPICall_PHP.png

Since PHP does not provide a way to transfer objects between PHP files, we will retrieve the access/refresh token we stored in the session variable earlier and update the DataService object using the function below.

1
2
3
<?php
$accessToken = $_SESSION['sessionAccessToken'];
?>

But first, create a DataService object. This DataService object is then used to get CompanyInfo data. Refer to the code here

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
function makeAPICall()
{

    // Create SDK instance
    $config = include('config.php');
    $dataService = DataService::Configure(array(
        'auth_mode' => 'oauth2',
        'ClientID' => $config['client_id'],
        'ClientSecret' =>  $config['client_secret'],
        'RedirectURI' => $config['oauth_redirect_uri'],
        'scope' => $config['oauth_scope'],
        'baseUrl' => "development"
    ));

    /*
     * Retrieve the accessToken value from session variable
     */
    $accessToken = $_SESSION['sessionAccessToken'];

    /*
     * Update the OAuth2Token of the dataService object
     */
    $dataService->updateOAuth2Token($accessToken);
    $companyInfo = $dataService->getCompanyInfo();

    print_r($companyInfo);
    return $companyInfo;
}

$result = makeAPICall();

?>

Important

Congratulations

You are all set to develop your app with Quickbooks.

More documentation
Once you’re up and running with the PHP SDK, you’ll find code samples using the latest version in our REST API docs and in the documentation for every Quickbook Online Entity. You can also find the samples in the latest PHP SDK here.
Getting help
We’d love to hear your feedback on the PHP SDK, and help you past any issues you may encounter. Feel free to Open a Github Issue, and we’ll make sure to get you sorted!