Note
Note
This step can be skipped for running the Hello, World! sample app.
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 |
composer.json
):
1 2 3 4 5 | { "require": { "quickbooks/v3-php-sdk": ">=*" } } |
1 2 3 | <?php require 'vendor/autoload.php'; ?> |
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.
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.
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 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', ) ?> |
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.
Click the Connect to QuickBooks button that you embedded into your app above. This initiaties the user authorization flow on this AuthorizeEndpoint:
callback.php
. You can see the file here.
1 | $parseUrl = parseAuthRedirectUrl($_SERVER['QUERY_STRING']); |
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; } ?> |
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.
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.