Using Backlight 3 JSON API from PHP, take 2

This is an update to my original post. It improves on error handling and has some other cosmetic changes.

One of the new features of Backlight 3 is a JSON API. It is a standard REST API interface and is very easy to use. The documentation shows all the endpoints and provides even a JavaScript and a jQuery library. How cool is that?

If you want to use it with PHP, I have a simple example for you. It doesn’t do much, but it shows you how to get started.

I’m using a helper function to fetch the REST API response for my query and return it as an array. If an error is captured, an error response is returned.

// Returns the JSON/REST API response as an array. If an error was detected
// an error response is returned
// Error response:
// (
//     ['status'] => 'error'
//     ['code'] => error number
//     ['message'] => error description
// )
function dlp_get_json_response($url){
   $curl = curl_init();
   curl_setopt_array($curl, array(
      CURLOPT_URL => $url,
      CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
   $posts = curl_exec($curl);

   // Was an error detected?
   if (curl_errno($curl)>0){	 
      $response = Array();
      $response['status'] = 'error';
      $response['code'] = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
      $response['message'] = curl_errno($curl).': '.curl_error($curl);
   } else {
      $json = json_decode($posts, true);
      if (json_last_error() > 0){
         // Detected json error	
         $response = Array();
         $response['status'] = 'error';
         $response['code'] = json_last_error();
         $response['message'] = 'JSON: '.json_last_error_msg();
         $response['response'] = $posts;			
      } else {
         $response = $json;
   return $response;	

The $url argument contains the link to your site including the desired endpoint such as https://yoursite.domain/backlight/api/get_album/12345. ‘12345’ is the Album Id. You can get it by using the JSON API or from the address bar when looking at the album in Backlight Publisher.

This example displays the number of photos contained in an album in the page footer:

function footer_bottom() {
   $posts = $this->dlp_get_json_response("https://yoursite.domain/backlight/api/get_album/12345");

   // check for errors 
   if (array_key_exists('status', $posts)){
      // error detected:
      echo '<pre>';
      echo '</pre>';
   } else {
      // Display image count
      echo "Array length: " . count($posts['album']['photos']);

In order for this example to work, you need to enable the JSON API. This can be done in Backlight => Admin => Settings. Just look for JSON API and enable it.

Have fun!

Did you like this post? Did you use the given code? Please consider supporting me by buying me a coffee!

Buy Me A Coffee


Leave a Reply

Your email address will not be published. Required fields are marked *

Web Design Consulting

Do you need help designing your web site or getting Backlight working the way you want? Contact me to discuss your idea or project.

Buy me a Coffee

If you like what I share here, please consider buying me a coffee or a print. This helps keeping me motivated to continue posting. Thank you!

Buy Me A Coffee