Synchronous calls

The following topics describe how to call synchronous methods on the following types of APIs of the QuickBooks Online PHP SDK:

Data Services APIs

The PHP SDK supports calling the QuickBooks Online APIs with synchronous methods. These APIs enable apps to access QuickBooks Onlineresources such as accounts, customers, and invoices.

Note

Note

The PHP SDK supports globalization. Your app users can input data in languages such as Chinese or Japanese to obtain output in the same languages.

1. Reference the SDK classes

Include the following require statements:

1
2
3
4
5
6
7
8
9
<?php
require "vendor/autoload.php"

use QuickBooksOnline\API\Core\ServiceContext;
use QuickBooksOnline\API\DataService\DataService;
use QuickBooksOnline\API\PlatformService\PlatformService;
use QuickBooksOnline\API\Core\Http\Serialization\XmlObjectSerializer;
use QuickBooksOnline\API\Facades\Customer;
?>

2. Set up configuration

The SDK allows you to define custom configuration for features such as logging. You can change these settings by editing the sdk.config file in the root.

  1. Specify the request and response formats. JSON serialization is not supported by the PHP SDK at this time.
  2. Specify the base URLs that the SDK uses. You should not need to modify these values.
  3. Specify whether to enable logging and the location to write the log files.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8" ?><configuration>
   <intuit>
      <ipp>
         <!--Json serialization not supported in PHP SDK v1.0.0 -->
         <message>
            <request serializationFormat="Xml" compressionFormat="None"/>
            <response serializationFormat="Xml" compressionFormat="None"/>
         </message>
         <service>
            <baseUrl qbo="https://quickbooks.api.intuit.com/" ipp="https://appcenter.intuit.com/api/" />
         </service>
         <logger><!-- To enable/disable Request and Response log-->
            <requestLog enableRequestResponseLogging="false" requestResponseLoggingDirectory="/IdsLogs" />
         </logger>
      </ipp>
   </intuit>
</configuration>

3. Build the ServiceContext object and create the DataService

To create a service context, create an instance of the DataService class, and pass the OAuth information as an array:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<?php
use QuickBooksOnline\API\DataService\DataService;

$dataService = DataService::Configure([
 'auth_mode' => 'oauth2',
 'ClientID' => "Client ID",
 'ClientSecret' => "Client secret",
 'accessTokenKey' => 'Access token key',
 'refreshTokenKey' => "Refresh token key",
 'QBORealmID' => "Realm ID",
 'baseUrl' => "development"
]);
?>

4. Create the data object

Data objects (entities) represent QuickBookscompany data, such as invoices and customers. The following code shows how to create a Customer object:

1
2
3
4
5
6
7
<?php
$customerObj = new IPPCustomer();
$customerObj->Name = "Name" . rand();
$customerObj->CompanyName = "CompanyName" . rand();
$customerObj->GivenName = "GivenName" . rand();
$customerObj->DisplayName = "DisplayName" . rand();
?>

5. Call the service

To perform a CRUD operation on QuickBooks Online data, call the appropriate method on the service. The following code snippets show how to perform CRUD operations by calling PHP methods on the service. For more examples, see the facade samples in the PHP SDK repository and the PHP CRUD samples on IntuitDeveloper.

Add() method

1
2
3
<?php
$resultingCustomerObj = $dataService->Add($customerObj);
?>
Update() method

This operation updates all writable properties of an existing entity:

1
2
3
<?php
$resultingCustomerObj = $dataService->Update($customerObj);
?>

To update only the property values specified in the request and leave the rest of writable properties unchanged, set the sparse property to true:

1
2
3
<?php
$resultingCustomerObj->sparse = 'true';
?>
FindById() method

1
2
3
<?php
$resultingCustomerObj = $dataService->FindById($customerObj);
?>
FindAll() method

To paginate through all of the objects of a specific type in a given company, call the FindAll() method. Increment the startPosition parameter with each successive call. The maxResults parameter is the number of objects to fetch in each call. For example, the following code snippet gets the first 10 customers:

1
2
3
4
5
<?php
$startPosition = 1;
$maxResults = 10;
$allCustomers = $dataService->FindAll('Customer', $startPosition, $maxResults)
?>
Delete() method

1
2
3
<?php
$dataService->Delete($invoiceObj);
?>

Name list entities, such as Customer and Vendor, cannot be deleted using the Delete() method. To delete a name list entity, set the Active flag to false, and use Update() to update the entity.

Void() method

1
2
3
<?php
$dataService->Void($customerObj);
?>
Batch process

To make a batch request, first create a $batch object, then add a query to the batch request using the AddQuery() method and an operation to the batch request using the AddEntity() method:

1
2
3
4
5
6
7
<?php
$batch = $dataService->CreateNewBatch();
   $batch->AddQuery("select * from Customer startPosition 0 maxResults 20", "queryCustomer",
      "uniqueQuery");
   $batch->AddEntity($invoice, "uniqueIdentifier", "create");
   $batch->Execute();
?>

Both the AddQuery() and AddEntity() methods take a string as the second parameter to uniquely identify the operation. The AddEntity() method requires a third parameter to specify the operation name: create, update, or delete.

The result of the batch request will be contained in the intuitBatchItemResponses object. Use uniqueIdentifier to find the specific batch response:

1
2
3
<?php
$batchItemResponse = $batch->intuitBatchItemResponses["uniqueIdentifier"];
?>