Skip to content
This repository has been archived by the owner on Dec 6, 2019. It is now read-only.

Example Usage in PHP

benbalter edited this page Jul 13, 2012 · 1 revision

Output list of agencies in PHP

<?php
//get list of all agencies
$data = json_decode( file_get_contents( 'https://raw.github.com/GSA/digital-strategy/1/agencies.json' ) );

//loop through each agency and output name with link to homepage
foreach ( $data->agencies as $agency ) { ?>
     <a href="http://<?php echo $agency->url; ?>"><?php echo $agency->name; ?></a><br />
<?php } ?>

Retrieve and parse an agency's report in PHP

<?php

//retrieve JSON report file
$data = json_decode( file_get_contents( 'https://agency.gov/digitalstrategy.json' ) );

//loop through each action item within the report outputting basic info
foreach ( $data->items as $item ) { ?>
     ID: <?php echo $item->ID; ?>
     Text: <?php echo $item->text; ?>
     Due: <?php echo $item->due; ?>
     <?php //loop through each field within the action item and output 
      foreach ( $item->fields as $field )  { ?>
          <?php echo $field->label; ?>: <?php echo $field->value; ?>
     <?php } ?>
<?php } ?>

Retrieve all agency digital strategy progress reports in PHP

<?php

//init master array to store reports
$reports = array();

// get list of all agencies and loop
$data = json_decode( file_get_contents( 'https://raw.github.com/GSA/digital-strategy/1/agencies.json' ) );
foreach ( $data->agencies as $agency ) {
     
     //grab file dynamically
     $report = file_get_contents( "http://{$agency->url}/digitalstrategy.json" );
     
     //account for file not existing, HTTP errors, etc.
     if ( !$report )
          continue;
     
     //verify valid json and decode
     $report = json_decode( $report );
     if ( !$report )
           continue;
     
     //push report into master array for later use   
     $reports[ $agency->id ] = $report;

}

// do something with all reports here using the $report array
?>