Download the PHP package incraigulous/contentful-sdk without Composer
On this page you can find all versions of the php package incraigulous/contentful-sdk. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package contentful-sdk
DEPRECATED. Use Contentful's official PHP SDK.
Contentful SDK for PHP
A modern PHP SDK for Contentful delivery and management APIs.
This is a framework agnostic PHP SDK.
Looking for the Laravel Toolkit?
Click Here for Laravel facades, a base repository, out-of-the-box caching and cache management, webhook creation by command and more.
New to Contentful? Contentful is an API driven CMS. Check out their website and API documentation.
Installation
Install via composer by running:
``
How to use it
Content Delivery API
Initializing
Create a new instance of the delivery SDK:
``
Token: Your Contentful API token. You can generate this using the Contentful control panel.
Space ID: (optional) The space to use for calls made by the instance. To use multiple spaces, you should use multiple SDK instances. If you don't provide a space ID, you'll only be able to use the spaces
resource.
Cacher: (optional, Incraigulous\ContentfulSDK\CacherInterface Implementation) An object to handle caching. This is optional (and easy to implement), so don't let it scare you off. Check out the caching section for more details.
Spaces
Getting all spaces
``
Getting a space
``
Content Types
Getting a content type
``
Searching content types
``
or
``
See Search Parameters to learn how to search for specific things.
Entries
Get an Entry by ID
``
Searching for entries
``
Including linked entries in search results
``
includeLinks
takes the number of link levels you want to include.
Assets
List all assets
``
Get an asset by ID
``
Searching for assets
``
Search Parameters
Searching is easy! Just call one or more of the following methods on your resource. The use of where() and limit() on the assets resource as shown above is a great example of searching.
Method | Parameters | Description |
---|---|---|
find | id | Limit results by ID |
where | field, operator, value | Limit results by operator search |
full | search | Alias for query |
query | search | Search all records for text match. See filters below. |
order | orderBy, $reverse = false | Order results by a field |
limit | number | Limit results by a quantity |
skip | number | Skip a quantity of results |
Where Filter Operators
Accepted Values | Produces Contentful Operator | Description |
---|---|---|
= | = | Equality: Search for exact matches |
!=, [ne], ne | [ne] | Inequality: Search for exact matches |
[in], in | [in] | Inclusion: Search for multiple matches |
[nin], nin | [nin] | Exclusion: Search for multiple matches |
<, [lt], lt | [lt] | Less Than: Number & date ranges |
>, [gt], gt | [gt] | Greater Than: Number & date ranges |
>=, [gte], gte | [gte] | Greater Than or Equal To: Number & date ranges |
match, [match] | [match] | Match: Full-text search on a specific field |
near, [near] | [near] | Near: Search for location near position |
within, [within] | [within] | Within: Search for location within bounding rectangle |
Anything not listed | ['YOUR OPERATOR HERE'] | Default |
For more detailed documentation on searching, see Contentful's API documentation.
Caching
I recommend you cache all Delivery API GET request results (i.e. Memcached or Redis). If you're working with a low traffic website you might be able to get by without caching, but it will greatly improve performance in any case.
One way would be to wrap all your API GET calls in a cache check, but that could lead to code duplication. Lucky for you, there's a better way.
I've included a cacher interface. Don't worry, it only requires four methods. Just build a class that implements Incraigulous\ContentfulSDK\CacherInterface
and pass it as the third parameter when you instantiate the SDK. The SDK will handle the rest. Each GET request will be cached with a unique key built from the query using your cacher class.
``
Here's an example using Laravel's Cache helper.
If you build a generic PHP cacher implementation for Redis or Memcached, submit a pull request. I would love to include it in the repo.
Management API
Initializing
Create a new instance of the management SDK:
``
Token: Your Contentful OAuth token.
Space ID: (optional) The space to use for calls made by the instance. To use multiple spaces, you should use multiple SDK instances. If you don't provide a space ID, you'll only be able to use the spaces
resource.
Note that there is a third parameter for the cacher. Management API calls are not currently cached to make sure that
GET
requests are up to date to avoid conflicts when updating records. I still wanted to allow a place to provide the cacher just in case I decide to use it for anything. Feel free to ignore it, or provide a cacher for good measure.
Using Builder Objects
Contentful payloads can be complex, especially when working with entries
or assets
. Payload builders are here to help! They're optional helper classes to assist with building payloads. They take care of building payload arrays for you and let you know what options you have along the way. They're especially helpful if your IDE has hinting.
If you don't want to use them, don't use them. The SDK parases the payload looking for builder objects and turns them into arrays before passing requests on to Contentful. As such, you are free to use standard arrays, or arrays made up of payload builder objects.
The following examples will assume you're using the builders you need:
``
Spaces
Creating a space
``
Updating a space
``
Deleting a space
``
Content Types
Creating a content type
``
Creating a content type with validation and links
``
Updating a content type
``
Publishing a content type
``
Unublishing a content type
``
Deleting a content type
``
Content Type Payload Builder
Parameters | Description |
---|---|
name | The content type name |
displayField | the title field key |
contentTypeFields | an array of content type fields. Can be a pure array or an array of ContentTypeField builder objects. |
Content Type Field Payload Builder
Parameters | Description |
---|---|
id | The content type field id |
name | The content type field name |
type | The content type field type |
required | (default: false) is the field required |
localized | (default: false) is the field localized |
Methods | Parameters | Description |
---|---|---|
setLink | linkType (default: Entry) | Add a link to the field |
setMultiLink | linkType (default: Entry) | Add multiple links to the field |
setValidations | validations | A validations array or a contentTypeValidations payload builder object |
Content Type Field Validations Payload Builder
Takes no parameters.
Methods | Parameters | Description |
---|---|---|
size | min (default: null), max (default: null) | Validates that the size of a text, object or array is within a range. |
in | set (array) | Validates that the value of a field belongs to a predefined set. It's defined specifying the elements that form the valid set. |
regexp | $pattern, $flags (default: null) | Validates that the value of a field matches a Regular Expression. |
dateRange | min (default: null), max (default: null) | Validates that the value of a field is within a date range. |
assetFileSize | min (default: null), max (default: null) | Validates that the size of an asset is within a range. |
assetImageDimensions | height (default: null), width (default: null) | Validates that the dimensions of an image are within a range. |
Entries
Creating an entry
``
Note that you are able to add content to a field without specifying a language. The default default language is en-US
. To specify a different default language, define a CONTENTFUL_DEFAULT_LANGUAGE
constant.
Example:
``
Creating an entry while specifying an ID
``
Creating an entry with a linked field
``
Updating an entry
$entry = $managementSDK->entries() ->find('ENTRY_ID') ->get();
$entry['fields']['title'] = new EntryField('title', 'Cheese is Healthy!'); $result = $managementSDK->entries()->put('ENTRY_ID', $entry);
Publishing an entry
$entry = $managementSDK->entries()->find('ENTRY_ID')->get(); $result = $managementSDK->entries()->publish('ENTRY_ID', $entry);
Unpublishing an entry
$entry = $managementSDK->entries()->find('ENTRY_ID')->get(); $result = $managementSDK->entries()->unpublish('ENTRY_ID', $entry);
Archiving an entry
$result = $managementSDK->entries()->archive('ENTRY_ID');
Unarchiving an entry
$result = $managementSDK->entries()->unarchive('ENTRY_ID');
Deleting an entry
$result = $managementSDK->entries()->delete('ENTRY_ID');
Entry Payload Builder
Parameters | Description |
---|---|
fields | An array of entry fields. Can be a pure array or an array of EntryField builder objects. |
Entry Field Payload Builder
Parameters | Description |
---|---|
name | The field name |
content | The field content |
language | (default: en-US or CONTENTFUL_DEFAULT_LANGUAGE constant) The default field language. |
Methods | Parameters | Description |
---|---|---|
addLanguage | languageKey | Add a language to the field. |
addLink | id, linkType (default: Entry), languageKey (default: en-US or CONTENTFUL_DEFAULT_LANGUAGE constant) | Create a relationship to a resource. |
addMultiLink | id, linkType (default: Entry), languageKey (default: en-US or CONTENTFUL_DEFAULT_LANGUAGE constant) | Create relationships to resources. |
Assets
Creating an asset
``
Creating an asset while specifying an ID
``
Updating an asset
``
Processing an asset
``
Publishing an asset
``
Unpublishing an asset
``
Archiving an asset
``
Unarchiving an asset
``
Deleting an asset
``
Asset Payload Builder
Parameters | Description |
---|---|
fields | An array of asset fields. Can be a pure array or an array of AssetField builder objects. |
Asset Field Payload Builder
Parameters | Description |
---|---|
name | The field name |
content | The field content |
language | (default: en-US or CONTENTFUL_DEFAULT_LANGUAGE constant) The default field language |
Methods | Parameters | Description |
---|---|---|
addLanguage | languageKey | Add a language to the field. |
File Payload Builder
Parameters | Description |
---|---|
contentType | The file content type |
fileName | The file name |
upload | The upload path |
language | (default: en-US or CONTENTFUL_DEFAULT_LANGUAGE constant) The language |
Webhooks
Creating a webhook
``
Updating a webhook, or creating new a webhook with a set ID
``
Deleting a webhook
``
Webhook Payload Builder
Parameters | Description |
---|---|
url | The callback URL for the webhook |
What's not implemented?
- Synchronization.
- The Preview API.
- Organizations.
Contributing
See a typo or a bug? Make a pull request.
What a new feature? Make a pull request.
Want a new feature and don't know how to build it? You can always ask, I might be game if I think it's a good enough idea.
How you can help
I'm going to make issues for features that I would like implemented. I'll assign them to myself if I plan to take to build them out. If I leave them unasigned, please step up and take it!