Fork me on GitHub
Skip to page content
 

PHP GOV Data SDK Sample

Overview

This sample will provide the steps to create a simple PHP application that can consume GOV Data. Following these steps in order will allow your application to operate correctly. Download this project.


Create a New Project

For this sample we are going to create a web site that will consume and list Agency data.
Create a project folder for this sample.
Create a new text document using any text editor and save it as Sample.php in the project folder.

Requirements

The GOV Data SDK has some library dependencies. Ensure that your version of PHP was compiled with support for cURL functions.

Add SDK to Project

Copy the SDK file into the project folder. You can get this from our GitHub Repository.

Locate Dataset Path

Each Dataset has a Dataset Location path that gives the URL that will be needed to reach the Dataset on the API. For the standard GOV Agency dataset sample please use http://api.dol.gov/V1/DOLAgency.

For the GOV Service Operation sample please use http://api.dol.gov/V1/SummerJobs.

Define the User Interface

Create the function resultsTable(); which will take the results returned by the data request and display them in a simple table.

function resultsTable($results) {
	$string = ‘’;
	$string .= “<table border=’1’ cellspacing=’0’><tr><th>Agency</th><th>Agency Full Name</th></tr>\n”;
	foreach ($results as $object) {
		$string .= “<tr><td>{$object->Agency}</td><td>{$object->AgencyFullName}</td></tr>\n”;
	}
	return $string . “</table>\n”;
}


For GOV Service Operation (Beta Version).

  • Include a JavaScript file in page , this javascript file has required jquery function to parse the json response.
  • <HTML>
    <head><script src="jquery-1.5.1.min.js" type="text/javascript"></script></head>
    <BODY>

  • Create the function resultsTable() for GOV Service Operation (Beta Version).
  • <?php
    function resultsTable($results) {
    	$string = '';
    	$string .= "<script type=\"text/javascript\">l;\n";
    	foreach ($results as $object) {
    		$object = str_replace('\\n', '', $object);
    		$object = str_replace('\"', '"', $object);
    		$object = trim($object, '\"');
    		$string .= "var jsonData = {$object};\n";
    	}
    	$string .= "</script>\n";
    	return $string;
    }


Implementing the GOV Data Request

The GOVDataSDK.php file handles everything required for submitting data requests to the GOV API, so we need to add them to Sample.php with an include statement.

include "GOVDataSDK.php";

Configure Security

Secure logins are provided using the following code; we will need to insure that the GOVDataContext instance is initialized with both an ApiKey and a SharedSecret. Replace "your-token" and "your-shared-secret" in new GOVDataContext() with your Token and SharedSecret.

  • The ApiKey corresponds to the Token identifier in your token list.
  • The SharedSecret that is associated with that token.

$context = new GOVDataContext('http://api.dol.gov', 'your-token', 'your-shared-secret');
$request = new GOVDataRequest($context);

Next, we need to call callAPI() on our GOVDataRequest. This method takes two arguments: the Dataset Location path to the table we are requesting data from, and an Array of arguments to pass to the data request.
For Standard GOV API datasets :

$results = $request->callAPI('DOLAgency/Agencies', Array('top' => '10', 'orderby' => 'Agency'));

For GOV Service Operation for Summer Jobs Plus:

$results = $request->callAPI('SummerJobs/getJobsListing', Array('format' => '\'json\'', 'query' => '\'farm\'', 'region' => '', 'locality' => '',
		'skipCount' => '1'));


If callAPI() returns a string, an error occurred while sending the request. Otherwise, callAPI() returns an Array of stdClass instances. In this sample, we will handle an error by echoing it to the screen. Otherwise, we will pass the results to our resultsTable() function.

if (is_string($results)) {
	echo $results . “\n”;
} else {
	echo resultsTable($results);
}


The finished code should look like this:

<HTML>
<BODY>
<?php
include “GOVDataSDK.php”;
 
function resultsTable($results) {
	$string = ‘’;
	$string .= “<table border=’1’ cellspacing=’0’><tr><th>Agency</th><th>Agency Full Name</th></tr>\n”;
	foreach ($results as $object) {
		$string .= “<tr><td>{$object->Agency}</td><td>{$object->AgencyFullName}</td></tr>\n”;
	}
	return $string . “</table>\n”;
}
 
$context = new GOVDataContext(‘http://api.dol.gov’, ‘your-token’, ‘your-shared-secret’);
$request = new GOVDataRequest($context);
 
$results = $request->callAPI(‘DOLAgency/Agencies’, Array(‘top’ => ’10’, ‘orderby’ => ‘Agency’));
 
if (is_string($results)) {
	echo $results . “\n”;
} else {
	echo resultsTable($results);
}
?>
</BODY>
</HTML>


Implementing the GOV Service Operation Request (Beta Version)

Please follow all above steps from Implementing the GOV Data Request section for GOV Service Operation Request and also add following javascript code to parse the json response using Jquery.

<script type="text/javascript">
$(document).ready(function () {
	var builder = [];
	for (var i = 0; i < jsonData.items.length; i++) {
		builder.push("<span>" + " " + jsonData.items[i].title + "</span><br/>");
	}
	
	$('p').html(builder.join(''));
	
});


The finished code should look like this:

<?php
function resultsTable($results) {
	$string = '';
	$string .= "<script type=\"text/javascript\">l;\n";
	foreach ($results as $object) {
		$object = str_replace('\\n', '', $object);
		$object = str_replace('\"', '"', $object);
		$object = trim($object, '\"');
		$string .= "var jsonData = {$object};\n";
	}
	$string .= "</script>\n";
	return $string;
}

$context = new GOVDataContext('http://api.dol.gov', 'your-token', 'your-shared-secret');
$request = new GOVDataRequest($context);

$results = $request->callAPI('SummerJobs/getJobsListing', Array('format' => '\'json\'', 'query' => '\'farm\'', 'region' => '', 'locality' => '',
		'skipCount' => '1'));
		
if (is_string($results)) {
	echo $results . “\n”;
} else {
	echo resultsTable($results);
}

?>

<script type="text/javascript">
$(document).ready(function () {
	var builder = [];
	for (var i = 0; i < jsonData.items.length; i++) {
		builder.push("<span>" + " " + jsonData.items[i].title + "</span><br/>");
	}
	
	$('p').html(builder.join(''));
	
});
</script>

View GOV Data

Build and run the application, and you should see something like the following:

Data

Result of GOV Service Operation Request (Beta Version):

Data