These integration guides are not official documentation and the Strapi Support Team will not provide assistance with them.
What Is PHP?
PHP (Hypertext Preprocessor) is a server-side scripting language built for web development. It powers around 74% of all websites that use server-side code. PHP’s popularity comes from its simplicity, flexibility, and rich library ecosystem.
PHP generates dynamic web pages and handles server-side tasks like form submissions, database queries, and file operations. The syntax is beginner-friendly but also supports advanced features for experienced developers.
PHP works well with databases such as MySQL, PostgreSQL, and SQLite, giving you flexibility in your stack. It integrates smoothly with web servers like Apache and Nginx, making it a reliable choice across hosting setups.
Why Integrate PHP with Strapi
Integrating PHP with Strapi gives you a powerful stack that combines efficient content management with PHP’s backend capabilities. Strapi’s API-first approach pairs well with PHP, offering a clear and scalable development model.
Here's why this integration works effectively:
- Separation of Concerns: Strapi handles content; PHP runs your business logic. Content editors work in Strapi’s interface, while you focus on the PHP codebase.
- Modern Admin Interface: Strapi includes a built-in admin panel for content editing. You don’t need to build CMS screens in PHP, which saves time and effort.
- Automatic API Generation: Strapi creates REST and GraphQL APIs for every content type, giving your PHP app immediate access to structured content.
- Flexible Content Modeling: You can define and update content types in Strapi without writing PHP migrations. This decouples content structure from app logic.
- Role-Based Access Control: Use Strapi’s permissions system to manage content roles alongside your PHP app’s authentication setup.
Strapi Cloud simplifies integration further by offering managed hosting and automatic updates. The release of Strapi v5 brings improved performance and a tighter focus on API design—benefits that apply whether you're using JavaScript, PHP, or any language consuming its APIs.
Keep in touch with the latest Strapi and PHP updates
How to Integrate PHP with Strapi
Running Strapi alongside PHP introduces technical challenges due to their different runtimes, similar to what you'd face during a headless CMS migration. Here's how to approach integration.
Technical Prerequisites
Before starting, you'll need:
- Node.js (version 14.x, 16.x, 18.x, or higher)
- NPM (6.x or higher recommended)
- PHP (version 7.4 or higher, 8.0+ preferred)
- Database: MySQL, PostgreSQL, SQLite, or MongoDB
- Server environment: Apache or Nginx for PHP deployment
- Minimum 4GB RAM for development environments
- At least 1GB of free disk space for installation and basic content
Deployment Strategies
Choose one of these deployment methods based on your infrastructure:
- Specialized Hosting: Use platforms like Platform.sh that support both PHP and Node.js in the same environment.
- Container-Based Deployment: Use Docker to isolate Strapi and PHP in separate containers while keeping API access intact.
- Separate Servers: Run Strapi on a Node.js-compatible host (e.g., Heroku) and PHP on traditional hosting. Connect them via API calls.
Explore available Strapi deployment options to choose the best fit for your stack.
API Configuration
Follow these steps to connect PHP with Strapi securely and reliably:
- Configure CORS in Strapi to allow only trusted domains to access your APIs.
- Set up authentication using JWT or API tokens for secure communication.
- Create content types in Strapi that map to your PHP application's data needs.
- Use REST or GraphQL endpoints based on your integration pattern.
- Define roles and permissions to control API access for different consumers.
Example of fetching data from Strapi in PHP:
1$ch = curl_init();
2curl_setopt($ch, CURLOPT_URL, "http://localhost:1337/api/articles");
3curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
4$response = curl_exec($ch);
5curl_close($ch);
6$data = json_decode($response, true);
7
8foreach ($data['data'] as $article) {
9 echo $article['attributes']['title'];
10}
This code fetches articles from Strapi’s REST API and displays their titles using PHP.
Keep in touch with the latest Strapi and PHP updates
Project Example: Integrating Strapi with PHP and Laravel
This example demonstrates how Laravel, a robust web application framework built on PHP, can be integrated with Strapi to create a scalable, content-driven web application.
Laravel leverages PHP’s core capabilities while providing structure, security, and modern development features. By combining Strapi’s headless CMS with Laravel, you can efficiently manage content and deliver it to your PHP-powered backend.
Project Overview
The agency needed to build a large-scale site with:
- Rapid development cycles
- Easy content management for non-technical users
- Efficient handling of dynamic, high-traffic content
They used Strapi as the headless CMS and Laravel as the backend to connect both through REST APIs. The team followed best practices for clean integration between the two systems.
Implementation Steps
To connect Strapi and Laravel effectively, the team followed a clear process. Each step focused on modular development, API consistency, and performance. Here's how they approached the integration:
1. Strapi Setup and Content Modeling
Strapi was set up as the content hub. The team created content types for articles, blogs, and products. Content creators managed content directly through Strapi’s admin panel.
2. API Configuration
Strapi automatically generates REST endpoints for each content type. Developers customized the responses by editing Strapi’s API output to match Laravel's data requirements.
3. Laravel Consumption
Laravel used the Guzzle HTTP client to retrieve data from Strapi:
1$client = new \GuzzleHttp\Client();
2$response = $client->request('GET', 'http://localhost:1337/api/articles');
3$articles = json_decode($response->getBody(), true);
Custom controllers in Laravel cached Strapi data to improve frontend speed.
4. Authentication and Security
JWT authentication was used to secure communication between Strapi and Laravel:
1$response = $client->request('POST', 'http://localhost:1337/api/auth/local', [
2 'json' => [
3 'identifier' => 'user@example.com',
4 'password' => 'your_password'
5 ]
6]);
7$token = json_decode($response->getBody(), true)['jwt'];
The token was included in subsequent requests to protected API endpoints.
5. Internationalization and Localization
The team managed multilingual content by using Strapi’s i18n plugin. Laravel retrieved localized entries based on the user's language preferences.
Outcomes and Benefits
This integration delivered strong results:
- Faster Development: Strapi’s built-in content modeling and auto-generated endpoints reduced development time.
- Scalability: The decoupled setup allowed the CMS and app to scale independently. Laravel’s caching improved performance.
- Empowered Editors: Non-technical users updated content directly through Strapi, freeing developers from routine content tasks.
- Maintainability: The modular architecture made it easy to add new features. Strapi plugins added extra functionality without major refactors.
Strapi Open Office Hours
If you have any questions about Strapi 5 or just would like to stop by and say hi, you can join us at Strapi's Discord Open Office Hours, Monday through Friday, from 12:30 pm to 1:30 pm CST: Strapi Discord Open Office Hours.
For more details, visit the Strapi documentation and the PHP documentation.