I design .

I'm Ketan ‐ A Melbourne web designer, developer and illustrator with 20 years industry experience.

Contentful RESTful API CodeIgniter Library

10 December 2017

Below is a simple CodeIgniter Contentful CMS library that I created for this website. It connects to the REST API and returns entries, assets or a single entry.

It's really easy to configure and can be customised as needed. As with any CI library, it should be placed within application/library (saved as Contentful.php).

Add your space ID and tokens within the construct() - you can split this out into a separate config file if you prefer.

<?php
class Contentful {

  function __construct() {
    // Space Id
    $this->space_id = '';

    // Content Delivery (Published) Token
    $this->access_token = '';

    // Content Preview (Draft) Token
    $this->access_token_preview = '';

    // Endpoint for Content Delivery (Published)
    $this->base_url = sprintf('https://cdn.contentful.com/spaces/%s/', $this->space_id);

    // Endpoint for Content Preview (Draft)
    $this->base_url_preview = sprintf('https://preview.contentful.com/spaces/%s/', $this->space_id);
  }

The get() method below is the main API call that can be used to return a list of entries, assets or a single entry. Passing an array of parameters allows you to filter the response.

/*
|--------------------------------------------------------------------------
| Get Content
|--------------------------------------------------------------------------
|
| Single function to return JSON content from Contentful API.
|
| @param $method (string) - E.g. 'entries', 'entry', 'assets' etc
| @param $params (array) - Array of parameters to pass as url queries
| @param $preview (bool) - If true, uses the preview endpoint
|
*/
public function get($method=false, $params=array(), $preview=false) {

    if ($method !== false) {

        // If $preview is set to true, use the content preview endpoint
        // and preview access token
        if ($preview === true) {
            $this->base_url = $this->base_url_preview;
            $this->access_token = $this->access_token_preview;
        }

        // Add the access_token to the endpoint
        $url_params = sprintf('?access_token=%s', $this->access_token);

        if (!empty($params) && is_array($params)) {
            $url_params .= '&'.http_build_query($params);
        }

        $endpoint = sprintf('%s%s/%s', $this->base_url, $method, $url_params);
        $response = file_get_contents($endpoint);

        if (!empty($response)) {
            $data = json_decode($response, true);
            return $data;
        } else {
            return false;
        }

    } else {
        return false;
    }

}

The get_entry() method below returns a single entry based on the slug. I've called my field urlHandle, so you'll need to change the 'fields.urlHandle' line to reference your particular slug field.

/*
|--------------------------------------------------------------------------
| Get Entry
|--------------------------------------------------------------------------
|
| Returns content for the passed entry (url handle).
|
| @param $content_type (string) - As set in Contentful
| @param $handle (string) - URL handle/slug of the entry to return
| @param $preview (bool) - If true, will use the content preview endpoint
|
*/
public function get_entry($content_type, $handle, $preview=false) {

    $params = array(
        'content_type' => $content_type,
        'fields.urlHandle' => $handle
    );

    $response = $this->get('entries', $params, $preview);

    if (!empty($response['items'])) {
        return $response['items'][0]['fields'];
    } else {
        return false;
    }

}

} // End of class

All methods are called using the usual CI syntax, for example:

$this->contentful->get('entries');

Have fun! (I did!)


Comment: via Twitter