PHP

The QuickBooks Online PHP SDK makes it easier 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.
Tutorial objective
This tutorial demonstrates how to:
  • Install the QuickBooks Online PHP SDK and required software
  • Create an app on the Intuit Developer portal
  • Connect to QuickBooks Online
  • Make a request to the QuickBooks Online API
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 the PHP SDK via Composer

Note

Note

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

The recommended way to install the PHP SDK is with Composer, which is a dependency management tool for PHP that allows you to declare the dependencies your project needs and installs them into your project. The Composer installation 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 the 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 Online company, you need to create an app on the developer portal.
  1. Sign into the developer portal. Select My Hub > App dashboard from the upper-right corner of the toolbar.
qbo/docs/develop/sdks-and-samples-collections/create-app-1-v1.png
qbo/docs/develop/sdks-and-samples-collections/create-app-1-v1.png

x

  1. In your apps dashboard, select the app card with a +.
qbo/docs/develop/sdks-and-samples-collections/create-app-2-v1.png
qbo/docs/develop/sdks-and-samples-collections/create-app-2-v1.png

x

  1. Select Get Started.
qbo/docs/develop/sdks-and-samples-collections/create-app-3-v1.png
qbo/docs/develop/sdks-and-samples-collections/create-app-3-v1.png

x

  1. Enter a name for your app and select Next.
qbo/docs/develop/sdks-and-samples-collections/create-app-4-v1.png
qbo/docs/develop/sdks-and-samples-collections/create-app-4-v1.png

x

  1. Select a scope, either Accounting or Payments, and select Done.
qbo/docs/develop/sdks-and-samples-collections/create-app-4-v1-2.png
qbo/docs/develop/sdks-and-samples-collections/create-app-4-v1-2.png

x

Note

Note

Creating a developer account also creates a QuickBooks Online test company (also referred to as a sandbox company). You can create up to 10 sandbox companies.

Connect to QuickBooks Online
To access the data of a QuickBooks Online 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. The steps in this section go over how to build this button.
  1. Development and Production keys can be found in the sidebar on the left. Select Keys and credentials.
qbo/docs/develop/sdks-and-samples-collections/create-app-5-v1.png
qbo/docs/develop/sdks-and-samples-collections/create-app-5-v1.png

x

  1. Locate your client ID and client secret.
qbo/docs/develop/sdks-and-samples-collections/create-app-6-v1.png
qbo/docs/develop/sdks-and-samples-collections/create-app-6-v1.png

x

  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 Settings to this URL.

    qbo/docs/develop/sdks-and-samples-collections/create-app-7-v1.png
    qbo/docs/develop/sdks-and-samples-collections/create-app-7-v1.png

    x

  2. Configure the client ID and client secret along with the redirect URL in your code’s config.php as shown here. View the config-sample.php sample here.

 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, ClientID, and RedirectURI to redirect the user to the AuthorizeEndpoint. A code snippet is shown here. To see the complete code file in github, go to 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 Connect to QuickBooks button, which initiates the authorization flow. You can see how this is implemented in the index.php sample code here.

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

    x

  2. Click the Connect to QuickBooks button that you embedded into your app above. This initiaties the user authorization flow on this AuthorizeEndpoint:

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

    x


  1. After the user clicks the Connect to QuickBooks button, the request is sent to an Intuit server. When the request is successfully processed, the Intuit server responds with an authorization code and QuickBooks Company ID (also called Realm ID) on the Redirect URI, which is handled by callback.php. You can see the file here.

1
$parseUrl = parseAuthRedirectUrl($_SERVER['QUERY_STRING']);
  1. This authorization code is exchanged for an access or refresh token using the TokenEndpoint. We set the tokens into a session variable. Access tokens are used in an API request; refresh tokens are used to get fresh short-lived access tokens after they expire. You can see 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 a QuickBooks Online API request

Once the tokens are received, they can be used to make QuickBooks Online API calls. The following shows a sample API call. You can see the apiCall.php script to make the call here.

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

x

Since PHP does not provide a way to transfer objects between PHP files, we retrieve the access or 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. You can see 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 now set to develop your app with QuickBooks Online.

More documentation
You’ll find code samples using the latest version and descriptions for every Quickbook Online entity in the REST API reference. You can also find samples in the PHP SDK here and in the samples gallery.
Getting help
We’d love to hear your feedback on the PHP SDK and assist you with any issues you may encounter. Feel free to open a Github issue.