Using Backlight 3 JSON API from PHP

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:

// Returns the JSON API response as an array
function dlp_get_wp_rest_response($url){
    $curl = curl_init();
     curl_setopt_array($curl, array(
       CURLOPT_RETURNTRANSFER => 1,
       CURLOPT_URL => $url,
       CURLOPT_USERAGENT => 'User Agent X'
     ));
     $posts = curl_exec($curl);
     curl_close($curl);
     return json_decode($posts, true);	
}

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 simply fetches the data for one album and then counts the number of images it contains:

$posts = $this->dlp_get_wp_rest_response("https://yoursite.domain/backlight/api/get_album/12345");
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


Thanks!

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

Categories