Lamplight API examples - organisation search

Any data shown on this page is fictious - this is an example!

The example

Search for organisations:

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. Firstly, we want some organisations - those that are 'service users' in Lamplight. We then use the form query parameters to add search by free-text, postcode or services offered. Note that services offered is a published custom field in Lamplight.

  // Set up the client to fetch all orgs
  if ($_GET['q']) {
    $client->setParameterGet('q', ($_GET['q']));
  if ($_GET['postcode']) {
    $client->setParameterGet('postcode', ($_GET['postcode']));
  if ($_GET['services']) {
    $client->setParameterGet('services_offered', ($_GET['services']));

And once that's all set, we can make the request:

    $orgResponse = $client->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, 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. The Lamplight_RecordSet can make this a lot easier (and more attractive from a code perspective) - the organisation listing example explains it in detail.

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.