Contentful + PHP
Updated: October 26, 2020
Content infrastructure for your next PHP project
Introduction
![Contentful + PHP headless CMS](http://images.ctfassets.net/fo9twyrwpveg/7cKShrysm8C6a8dZfNPQdw/4e3fdeec4258c2a822a5dea7ae8469c5/Contentful___PHP_Contentful___PHP.png)
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
We provide SDKs for popular programming languages like JavaScript, Ruby, and, of course, PHP. These SDKs are designed to make your life as a developer easier when using Contentful and give you almost instant access to our APIs and their features.
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.
To get started with our PHP CDA SDK, make sure you have Composer installed on your machine. You can then use Composer to install the Content Delivery API (CDA) SDK:
textcomposer require contentful/contentful
Then add the Composer autoloader to the top of your project:
textrequire_once ‘vendor/autoload.php’;
All interactions with the SDK go through
Contentful\Delivery\Client
Here’s an example that fetches content from Contentful using our Images API:
text<?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:
text<?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.
Don’t worry, we’ve got you covered. Contentful has integrations for two of the most popular PHP frameworks: Symfony and Laravel.
Symfony
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:
textcontentful: 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
ContentfulBundle
So you can see our
ContentfulBundle
Laravel
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:
text<?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.
What’s next
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.