Query filters

Individual queries

The query filters of the QuickBooks Online PHP SDK enable your app to retrieve entities whose properties meet specified criteria. For example, your app can retrieve only those customer entities that have been created within the last twenty days.

1. Reference the SDK classes

Include the following require statements:

1
2
3
4
5
6
7
<?php
require_once('../config.php');
require_once(PATH_SDK_ROOT . 'Core/ServiceContext.php');
require_once(PATH_SDK_ROOT . 'DataService/DataService.php');
require_once(PATH_SDK_ROOT . 'PlatformService/PlatformService.php');
require_once(PATH_SDK_ROOT . 'Utility/Configuration/ConfigurationManager.php');
?>

2. Build the ServiceContext object

The ServiceContext object is a parameter for all calls to QuickBooks Online Data Services, Platform Services, and Reporting Services. When you call a Data Service resource to access a QuickBooks company, the SDK first retrieves the available custom configuration settings to build the ServiceContext object.

  1. Create the OAuthRequestValidator object. A valid OAuthRequestValidator object ensures that the end-user has authorized your app to access QuickBooks Online data. The following code creates an OAuthRequestValidator object:

1
2
3
4
5
6
7
8
9
<?php
$accessToken = '';
$accessTokenSecret = '';
$consumerKey = '';
$consumerSecret = '';

$requestValidator = new OAuthRequestValidator(
$accessToken, $accessTokenSecret, $consumerKey, $consumerKeySecret);
?>
  1. Create an instance of the ServiceContext class. The following code creates a ServiceContext object for a QuickBooks Online API app:

1
2
3
4
5
<?php
$realmId = '';
$serviceType= IntuitServicesType::QBD;
$serviceContext = new ServiceContext($realmId, $serviceType, $requestValidator);
?>

3. Create the DataService

Create an instance of DataService by passing the ServiceContext created in Step 3 as the argument:

1
2
3
<?php
$dataService = new DataService($serviceContext);
?>

4. Query the service

To perform a query operation on QuickBooks Online data, call the appropriate method on the service. For an overview of the query operations available, see Data queries.

The following code snippets show how to perform a query operation by calling PHP methods on the service:

1
2
3
<?php
$entities = $dataService->Query("SELECT * FROM Customer");
?>
Multiple queries

They query filter feature of the PHP SDK supports batch querying. The following steps describe how to process queries in batch.

1. Reference the SDK classes

Include the following require statements:

1
2
3
4
5
6
7
<?php
require_once('../config.php');
require_once(PATH_SDK_ROOT . 'Core/ServiceContext.php');
require_once(PATH_SDK_ROOT . 'DataService/DataService.php');
require_once(PATH_SDK_ROOT . 'PlatformService/PlatformService.php');
require_once(PATH_SDK_ROOT . 'Utility/Configuration/ConfigurationManager.php');
?>

2. Build the ServiceContext object

ServiceContext is a parameter for all calls to QuickBooks Online APIs.

  1. Create the OAuthRequestValidator object:

1
2
3
4
<?php
$oauthValidator = new OAuthRequestValidator(
$accessToken, $accessTokenSecret, $consumerKey, $consumerKeySecret);
?>
  1. Create an instance of the ServiceContext class:

1
2
3
<?php
$context = new ServiceContext($appToken, $realmId, $IntuitServicesType->QBD, $oauthValidator);
?>

Note

Note

For more information, see Authorization and Configuration.

3. Create the query service

Create an instance of QueryService for customer entity by passing the ServiceContext created in Step 2 as the argument:

1
2
3
<?php
$customerQueryService = new QueryService($context);
?>

4. Create the batch object

1
2
3
<?php
$batch = $service->CreateNewBatch();
?>

5. Create the query

The following statement returns all customers with given name starting with letter T, such as Tom or Ted:

1
2
3
<?php
$query = customerContext.Where(c => c.GivenName.StartsWith("T")).ToIdsQuery();
?>

6. Add Request to the batch

Add the query request to the batch object created in step 4:

1
2
3
<?php
$batch->Add(query, "bID1", $OperationEnum->query);
?>

Note: Each batchItem holds an entity and is assigned a unique batch item ID, the bId property. The bId is referenced in the BatchResponse objects that are returned by the call to the service.

7. Execute the batch request

Call the service using the Execute() method to obtain an IntuitBatchResponse object:

1
2
3
4
5
<?php
$batch->Execute())
$queryResponse = $batch["bID1"];
List<Customer> customers = queryRes.Entities.Cast<Customer>().ToList();
?>