Contentful + PHP
Content infrastructure for your next PHP project
It’s no secret that managing an old-school, monolithic content management systems (CMS) comes with its fair share of burdens. Whether it’s worrying about scalability issues or babysitting dense database architecture, one thing is clear: Nobody has time for that. That’s why we’re proposing a new solution: an API-first, technology agnostic content infrastructure.
In this article, we’ll highlight the benefits of using Contentful and content infrastructure for your next PHP project.
What is Contentful?
Before we go any further, it’s important to clarify what you’ll be getting with Contentful. Through our content infrastructure, you’ll receive a platform that enables developers to fetch content with API calls, while offering editors a familiar-looking web app for creating and managing content.
So whether you’re developing for wearables, smartphones, or web apps, your content is just an API call away. And because it runs as a hosted solution in the cloud, it requires far less maintenance than a traditional PHP content management system—leaving you more time to focus on building great applications.
Differences between Contentful and other CMSs
When talking about PHP CMSs, developers are often limited to WordPress or Drupal. The problem with these legacy CMSs is that they are created around one type of content they’re optimized to display, usually websites, and most become simple web publishing tools.
Contentful, on the other hand, is completely adapted to your needs. You define your own content model, which is independent from any presentation layer. This model is what defines the kinds of content you want to manage. Then, you can take that content and create engaging experiences around it, while our CDN takes care of delivering content to your users 24/7.
Our PHP SDKs
Content Delivery API (CDA)
Contentful’s Content Delivery API (CDA) is a read-only API for delivering content from Contentful to apps, websites, and other media—arriving as JSON data and media files.
composer require contentful/contentful
Then add the Composer autoloader to the top of your project:
All interactions with the SDK go through
. To create a new client, you have to pass an access token and a space ID to the constructor. From there, you can adjust your query to receive the appropriate data.
Here’s an example that fetches content from Contentful using our Images API:
<?php $client = new Contentful\Delivery\Client('fdb4e7a3102747a02ea69ebac5e282b9e44d28fb340f778a4f5e788625a61abe', 'yadj1kx9rmg0'); $asset = $client->getAsset('wtrHxeu3zEoEce2MokCSi'); // This is the coresponding URL if you want to show the Image // http://images.contentful.com/yadj1kx9rmg0/wtrHxeu3zEoEce2MokCSi/cf6f68efdcf625fdc060607df0f3baef/quwowooybuqbl6ntboz3.jpg?w=300&h=300&fm=png&fit=crop&r=100 $options = (new Contentful\Core\File\ImageOptions()) ->setHeight(300) ->setWidth(300) ->setFormat('png') ->setResizeFit('crop') ->setRadius(20); $resultUrl = $asset->getFile()->getUrl($options);
And there’s so much more where that came from. If you want to dive deeper, we’ve explained this entire process in our getting started tutorial.
Content Management API (CMA)
We also recently released the beta version of the PHP SDK for Contentful’s Content Management API (CMA). This is a read-write API for managing content. You can use the CMA for several use cases, including automatic imports from CMSes like WordPress or Drupal and building custom editing experiences.
Once you have Composer installed and you include the autoloader (as outlined above), you can then work with the following code snippet to manage content from Contentful:
<?php $client = new Contentful\Management\Client('<content_management_api_key>'); $environment = $client->getEnvironmentProxy('<space_id>', '<environment_id>'); $entry = new Contentful\Management\Resource\Entry('<content_type_id>'); $entry->setField('title', 'en-US', 'Entry title'); $environment->create($entry); $entry->setField('title', 'en-US', 'New entry title'); $entry->update(); $entry->publish();
Integrations with Symfony and Laravel frameworks
You’re probably using a framework for your PHP applications—and for a good reason. Frameworks enable you to scale quickly, code maintenance is more manageable than with plain PHP, and they allow you to invest your time in the actual task instead of the supporting technology.
One of our PHP evangelists once described the Symfony Framework as “a bit like a beautiful soup base”. It’s made of excellent ingredients (well, components) and from that, you can make any kind of soup (application) you want. People love it, including Contentful client Spotify.
Here’s how you can configure your Contentful client the Symfony way:
contentful: delivery: space: cfexampleapi token: b4c0n73n7fu1
Along with this integration, we’ve also launched ContentfulBundle, an additional ingredient that you can add to your Symfony-based soup. The
integrates with Symfony Web Profiler, allowing you to fix bugs and performance issues on the integration point between your application and Contentful.
So you can see our
in action, we created a generic product catalog app connected to an example space using Symfony. You can find the full code on GitHub or follow our step-by-step guide to build it yourself.
Probably the most popular framework right now, Laravel is self-proclaimed as the “PHP Framework for Web Artisans.” With out-of-the-box REST API compatibility, unit-testing support, and a focus on speed of development, the framework is a natural fit with the Contentful platform.
Configuring your Contentful client using Laravel looks like this:
<?php return [ 'delivery.space' => 'cfexampleapi', 'delivery.token' => 'b4c0n73n7fu1', ];
To facilitate this collaboration, we produced the Contentful Laravel Package. This package empowers PHP developers to integrate the Contentful API into their applications built with the Laravel framework. Additionally, we developed a sample blogging app using the Content Delivery API with a supplementary tutorial.
We’re constantly developing new features and framework integrations for PHP. In fact, we have an entire team dedicated to it. But in the meantime, you can also check out the Contentful for PHP page for tutorials, example apps, and more information on other ways of using PHP with Contentful. Or if you’re ready to jump right in, create a free account today and try it for yourself.