PHP DOL Data SDK
Adding the SDK to Your Project
Using the SDK
Adding the SDK to Your Project
Add SDK files to your project
Extract the zipped SDK folder.
Copy the DOLDataSDK.php file into your project.
Requirements
This SDK makes use of PHP's cURL functions.
Using the SDK
Reference the DOL Data API in your source file
In the source files that will make DOL Data requests add the following include or require:
include "DOLDataSDK.php";Making API Calls
The DOLDataRequest class is used to make API calls. It requires a context object to be passed to its constructor in order to get the API key and URL information needed for the requests. The DOLDataContext is the context class used to hold this information.
You can also pass arguments to the API call by adding them to an Array as key/value pairs and passing it as the second argument of the callAPI() method. Valid arguments include top, select, filter, orderby, and skip. They are defined later in this document.
In order to make requests:
- Instantiate a DOLDataContext object
- Instantiate a DOLDataRequest object and initialize it with the context
- Create an Array and store all the API arguments as key/value pairs.
- Call the callAPI() method, passing the API name and the arguments (leave off the arguments if there are no arguments)
Sample code to make requests for standard DOL API datasets:
<?php
include "DOLDataSDK.php";
//Instantiate DOL Data context object
//This object stores the API information required to make requests
$context = new DOLDataContext("http://api.dol.gov", "your-api-key", "your-shared-secret");
//Instantiate new request object. Pass the context that contains all the API key info
$request = new DOLDataRequest $context;
//API you want to fetch data from
$method = "FORMS/Agencies";
//Build Array arguments
//Example to retrieve top 10 records and just get one field.
$arguments = Array('top' => '10', 'select' => 'AgencyName');
//Make API call
$results = $request->callAPI($method, $arguments);
if (is_string($results)) {
//handle error
} else {
//handle success
}
?>
Sample code to make requests for DOL Service Operation (e.g Summer Jobs Plus) :
<?php
include "DOLDataSDK.php";
//Instantiate DOL Data context object
//This object stores the API information required to make requests
$context = new DOLDataContext("http://api.dol.gov", "your-api-key", "your-shared-secret");
//Instantiate new request object. Pass the context that contains all the API key info
$request = new DOLDataRequest $context;
//API you want to fetch data from
$method = "SummerJobs/getJobsListing";
//Build Array arguments
//Example to retrieve top 10 records and just get one field.
$arguments = Array('format' => '\'json\'',
'query' => '\'Farm\'',
'region' => '',
'locality' => '',
'skipCount' => '1');
//Make API call
$results = $request->callAPI($method, $arguments);
if (is_string($results)) {
//handle error
} else {
//handle success
}
?>
The callAPI() triggers the SDK to process the request, add authorization headers and returns either an Array with the results or a String with an error message.
API Method Arguments
Standard DOL API datasets
top
The top argument is used to specify the number of records to be returned.
Example:
$args = Array('top' => '2');
skip
The skip argument specifies that you want the service to skip a specified number of results. This argument in combination with top, serve as the basis to implement pagination.
Example:
//Return records 21-30
$args = Array('top' => '10', 'skip' => '20');
select
The select argument allows you to specify the exact columns that you want the API to return. Separate them with commas if there is more than one.
In order to save bandwidth, it is always a good idea to add a select clause to most requests and retrieve the columns needed for a specific function. Some datasets can contain dozens of columns and the performance of your app can be impacted if you always return all of the columns. This is even more important in mobile applications because of device memory limitations, possible slow network speeds, and data usage caps imposed by carriers.
Example:
//Select the AgencyId and AgencyFormNumber columns
$args = Array('select' => 'AgencyId,AgencyFormNumber');
orderby
Use the orderby argument to sort the data by one or more columns. Separate them with commas if there is more than one.
//Order by the AgencyFormNumber column
$args = Array('orderby' => 'AgencyFormNumber');
filter
The filter argument is used to add filters to the API query. For example, you can filter results where a specific column is equal to a string provided.
The API recognizes the following comparison keywords:
eq – Equal to
ne – Not Equal to
gt – Greater than
lt – Less than
ge – Greater than or equal to
le – Less than or equal to
For string comparisons enclose any multiple word string in single quotes. For example: 'Legal Identity Report'
More than one filter can be specified by joining them with the and/or keywords. Separate filters with parenthesis.
For example: (AgencyId eq 'MSHA') and (Title eq 'Legal Identity Report') retrieves the records where the value in the AgencyId column is equal to 'MSHA' and the value for the Title column is equal to 'Legal Identity Report'.
Example:
$args = Array('filter' => "(AgencyId eq 'MSHA') and (Title eq 'Legal Identity Report')");
DOL Service Operation for Summer Jobs Plus
To use the Service Operations API the Operation name and parameters need to be supplied using the data services format. Each String typed parameter must be surrounded in quotes in order to work correctly. These quotes are then Url encoded and passed to the Service Operation.
format
The format argument allows you to specify the format that the results string will be returned. If format argument is not passed with request then result will be returned in json format by default. Following are the supported format of results string.
json ( Default )
xml
atom
Example:
$arguments = Array('format' => '\'json\'');
query
The query argument will be used to find matching job listings. This is required if region or locality are not provided.
Example:
$arguments = Array('query' => '\'Farm\'');
region
The region argument will be used to filter jobs listings by the state name or abbreviation. This is required if query or locality are not provided.
Example:
$arguments = Array('region' => '\'VA\'');
locality
The region argument will be used to filter jobs listings by The city name. This is required if query or region are not provided.
Example:
$arguments = Array('locality' => '\'Alexandria\'');
skipCount
The skipCount argument will be used to specify the number of records to skip ahead from the first recor.Valid range of 1-99 and it required.
Example:
$arguments = Array('skipCount' => '1');