Lamplight API examples - organisation details

The example

Organisation detail for Night Shelter


Id
7
Name
Night Shelter
Summary
The shelter offers emergency shelter during the coldest months. Volunteers always needed!
Date updated
2015-12-06 22:05:03
Address line 1
54 Example Street
Address line 2
Test Bridge Lane
Postcode
MK2 1AA
Lat
Lng
Northing
Easting
Email
ns@lamplightdb.co.uk
Phone
020 7558 8793
Web
http://www.lamplightdb.co.uk
CVS Member
NM (Non Member)
Description of Main Services
Providing Evening Accomodation to men and women. Hot meal provided. Key Workers available for further support
Number of example views

The code

As in all these examples, we're using the php Lamplight_Client class, which makes constructing requests a bit easier. First, set up the client

     $client = new Lamplight_Client('', array(
       'key'     => LAMPLIGHT_APIKEY,
       'lampid'  => LAMPLIGHT_ID,
       'project' => LAMPLIGHT_PROJECT
    ));   
    

This gives us an Http Client with the authentication parameters set (we use an include to define the LAMPLIGHT_APIKEY, LAMPLIGHT_ID and LAMPLIGHT_PROJECT constants). We don't need to pass a uri because the Lamplight_Client does it for us.

Next, tell it what we want to fetch, and make the request. $orgID is $_GET['id'] cleaned up and validated. The convenience methods fetchOrgs($role) and fetchOne() (and similar) simply add parameters to the uri constructed by Lamplight_Client. To add additional parameters we use the Zend_Http_Client methods setParameterGet($key, $value) or setParameterPost($key, $value).

$orgResponse = $client->fetchOrgs("user")
                      ->fetchOne()
                      ->setParamterGet('id', $orgID)
                      ->request();
    

The Zend_Http_Response returned (see Zend Framework API docs) has some useful methods: we use isError() and getStatus() to check we've got something back with a 200 response, and then getBody() to retrieve the json encoded data. It's then a simple step to decode it and iterate over the data returned to write out some data.

Again, most simply we just iterate through the data property once we've json_decoded the response body. In actual implementations you would most likely want to process the data a little more (for example, putting links on web addresses). Note that if data is missing, it won't be returned, in order to minimise the size of data, so you may also need to test for the fields returned.

    // Did it work?
    if (!$orgResponse->isError() && $orgResponse->getStatus() == 200) {


        // Get the json
        $jsonOrgs = $orgResponse->getBody();
  
        // and decode it as a stdClass object:
        require_once 'Zend/Json.php';
        $orgList = Zend_Json::decode($jsonOrgs, Zend_Json::TYPE_OBJECT);
        $org = $orgList->data;

        // Now show it:
        echo "<h3>Organisation detail for " . htmlentities($org->name, ENT_QUOTES, "UTF-8") . "</h3>";


        // And now we can iterate through the object, using the properties
        // returned (note $org->name above
        foreach($org as $field => $value) {
            // ...
        }
    }

It's easy here to see errors. You can change the id values, and if the organisation is not publishable, a 400 response code will be returned, with a json encoded error message giving details. More details on error messages can be found here.