How to use the BPOC Commons API Using PHP's json_decode() function

CONTEXT:

The Balboa Park Commons site uses a simple rest style API that allows the retrival of our data in a lightwieght JSON data format. There is no authentication required and the data cannot be changed or manipulated on the host side. Developers and enthusiasts may retrive the JSON data and manipulate however they see fit on the client side. In this example the JSON data for the page of a Featured Set from url http://www.balboaparkcommons.org/objectview/listview/14149813/Animals is retrieved, itenerated through and echoed out on to the page using two simple PHP function called "file_get_contents" and "json_decode".


Key Concept: pre-pend the (or any) URL after the domain name with "/json/" to retrieve the raw JSON data of the Featured set with an ID (pk) of "14149813" and the title "Animals". In order to display the physical assets (i.e the images) a seconday host is defined as the BPOC's DAMS server located at http://piction.bpoc.org/piction/ which is where all of our publicly available assests are stored.


CODE EXAMPLE:

  1. <?php
  2. define ('P_HOST', 'http://piction.bpoc.org/piction/');
  3.  
  4. $data = file_get_contents('http://www.balboaparkcommons.org/json/objectview/listview/14149813/Animals/');
  5. $json_data = json_decode($data, true);
  6.  
  7. /**
  8. * view the data object with nested arrays
  9. * print_r($json_data);
  10. */
  11.  
  12. echo '<h1>Featured Set: '.$json_data['n'].'</h1>';
  13. echo '<p>PK:'.$json_data['pk'].'</p>';
  14. echo '<p>Set Item Count: '.$json_data['num'].'</p>';
  15. echo '<br />';
  16.  
  17. for($i=0; $i<count($json_data['ct']); $i++) {
  18. echo '<img src="'.P_HOST.$json_data['ct'][$i]['w'}.'" /><br /><br /> ';
  19. }
  20.  
  21. ?>

CODE EXAMPLE OUTPUT:

Featured Set:

PK:

Set Item Count: