Get Started with PHP SDK

This guide will help you get started with Contentstack PHP SDK to build apps powered by Contentstack.

Prerequisites

To get started with PHP, you will need the following:

  • PHP version 5.5.0 or later

SDK Installation and Setup

To install the PHP SDK, choose either of the following methods:

Method 1: Use Composer

To install the PHP SDK in your project using Composer, fire up the terminal, point it to the project location, and run the following command:

composer require contentstack/contentstack

Method 2: Download the zip file

To download the PHP SDK, perform the following steps:

  1. Download the PHP SDK
  2. Move this downloaded zip file to a particular folder of your choice

Let's get started with the implementation.

Initialize SDK

Initialize the SDK by following either of the methods depending on the type of installation.

If installed using Composer:

To initialize the SDK, specify the API Key, delivery token, and environment name of your stack.

use Contentstack\Contentstack;
$stack = Contentstack::Stack(API_KEY, DELIVERY_TOKEN, ENV_NAME);

If installed using the zip file:

To initialize the SDK, specify the API key, delivery token, and environment name of your stack.

use Contentstack\Contentstack;
include_once "contentstack/contentstack.php";
$stack = Contentstack::Stack(API_KEY, DELIVERY_TOKEN, ENV_NAME);

Once you have initialized the SDK, you can start getting content in your app.

Basic Queries

Contentstack SDKs let you interact with the Content Delivery APIs and retrieve content from Contentstack. They are read-only in nature. The SDKs fetch and deliver content from the nearest server via Fastly, our powerful and robust CDN.

Get a Single Entry

To get a single entry, you need to specify the content type and the UID of the entry:

$result = $stack->ContentType(CONTENT_TYPE_UID)->Entry(ENTRY_UID)->toJSON()->fetch()
$result - entry object

Get Multiple Entries

To retrieve multiple entries of a content type, specify the content type uid. You can also specify search parameters to filter results:

$result = $stack->ContentType(CONTENT_TYPE_UID)->Query()->toJSON()->includeCount()->includeContentType()->find()
$result[0] - array of entries
$result[1] - content type
$result[2] - count of the entries

These were examples of some of the basic queries of the SDK. For advanced queries, refer to the Contentstack PHP SDK API reference.

Note: Currently, the PHP SDK does not support multiple content types referencing in a single query. For more information on how to query entries and assets, refer the Queries section of our Content Delivery API documentation.

Paginating Responses

In a single instance, the Get Multiple Entries query will retrieve only the first 100 items of the specified content type. You can paginate and retrieve the rest of the items in batches using the skip and limit parameters in subsequent requests.

$stack = Contentstack::Stack(API_KEY, DELIVERY_TOKEN, ENV_NAME);
$result = $stack->ContentType(CONTENT_TYPE_UID)->Query()->toJSON()->skip(20)->limit(20)->find()

Limitations

  • We have a URL size limitation of 8KB on API Requests that hit our CDN services. Any Request URL that goes above this size limit will receive the 400 - Bad request error response. Please make sure you limit the size of your API Requests.
  • The PHP SDK does not support multiple content types referencing in a single query.
  • Currently, the PHP SDK does not support retrieving details of Global Fields.

More Resources

Was this article helpful?

Thanks for your feedbackSmile-icon

On This Page

^