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
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.
Create an app on the Intuit Developer portal
To connect your application to a QuickBooks company, you need to create an app on the developer portal.
  1. Sign into the developer portal. From the menu at the top-right select My Apps.

View Screenshot

qbo/docs/develop/sdks-and-samples-collections/create-app-1-v1.png
  1. In the My Apps Dashboard create a new app by clicking the + Create an App button.

View Screenshot

qbo/docs/develop/sdks-and-samples-collections/create-app-2-v1.png
  1. Select QuickBooks Online and Payments.

View Screenshot

qbo/docs/develop/sdks-and-samples-collections/create-app-3-v1.png
  1. Enter a name for your app, select a scope (choose from Accounting or Payments), select the countries you want to accept connections from, and click on Create App.

View Screenshot

qbo/docs/develop/sdks-and-samples-collections/create-app-4-v1.png
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 click on the Connect to QuickBooks button.
  1. Development and Production keys can be found in the sidebar on the left. Under Development select Keys and OAuth.

View Screenshot

qbo/docs/develop/sdks-and-samples-collections/create-app-5-v1.png
  1. Locate your Client ID and Client Secret.

View Screenshot

qbo/docs/develop/sdks-and-samples-collections/create-app-6-v1.png
  1. During the authorization flow, your app is redirected to Intuit’s OAuth server to get the authorization code after validating the user’s username and password. This authorization code is sent to the redirect URI. For this tutorial, redirect users to http://localhost:8080/callback.php. Set the Redirect URI in the Keys & OAuth tab with this URL.

View Screenshot

qbo/docs/develop/sdks-and-samples-collections/create-app-7-v1.png
  1. 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',
)
?>
  1. 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',
)
?>
  1. 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
  1. 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

  1. 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']);
  1. 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;
}
?>
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!