Easily restore your project to a previous version with our new Instant One-click Backup Recovery

Headless CMS for PHP

Hygraph is the ideal Headless CMS for PHP websites and applications. Read further to learn how our API-first CMS allows you to add components to your PHP apps in minutes and enable your website's content to be managed from a powerful CMS.

Step #1 - Construct your query and fetch the data from Hygraph

In PHP, to fetch data from a GraphQL API, you first construct a GraphQL query as a string. This query specifies the data you want to retrieve. Next, you encode this query into a JSON payload. Using cURL, a PHP library for making HTTP requests, you set up a POST request to the GraphQL endpoint, include the JSON payload, and set the necessary HTTP headers, such as 'Content-Type: application/json' and any required authorization tokens.

Once the request is executed and a response is received, you can decode the JSON response and access the data you requested. If the request is successful, you process the data; if not, you handle any errors that occurred during the request.

// The GraphQL endpoint
$url = 'https://api-<region>.hygraph.com/v2/<some hash>/master';
// GraphQL query
$query = <<<GRAPHQL
query GetProductData {
products {
name
description
slug
availability
imageUrl
}
}
GRAPHQL;
// Create the payload
$data = json_encode(['query' => $query]);
// cURL setup
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer YOUR_HYGRAPH_TOKEN' // Replace with your actual auth token
]);
// Execute the cURL session
$response = curl_exec($ch);
// Close the cURL session
curl_close($ch);
// Decode JSON response
$result = json_decode($response, true);

Run mutations in PHP to store content in a headless CMS

In PHP, to perform a GraphQL mutation, you start by crafting a mutation string that describes the operation, such as adding or updating data. You then package this query, along with any necessary variables, into a JSON-encoded payload. This payload is sent to the GraphQL server using a cURL request, where you must set the appropriate HTTP headers, including the content type and any authorization details.

Once the server processes the mutation, it responds with a JSON object. You then decode this response in PHP to check for success or handle any errors, effectively completing the mutation process.

// Your GraphQL endpoint
$url = 'https://your-graphql-endpoint.com/graphql';
// GraphQL mutation and variables
$mutation = '
mutation CreateProduct($name: String!, $description: String!) {
createProduct(input: {name: $name, description: $description}) {
id
name
description
}
}';
// Prepare the JSON payload
$jsonData = json_encode([
'query' => $mutation,
'variables' => [
'name' => 'New Product',
'description' => 'This is a new product description.'
]
]);
// Initialize cURL
$ch = curl_init($url);
// Set cURL options
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $your_auth_token // Replace with your actual auth token variable
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the cURL session and fetch the response
$response = curl_exec($ch);
curl_close($ch);
// Decode the response
$responseData = json_decode($response, true);

Start building with PHP

We made it really easy to set up your project in Hygraph and use our GraphQL API within your PHP project.

Quickstart

Check out our docs to see how you can quickly set up your Hygraph project and enable the content API for your PHP website or app.

Learn GraphQL

Hygraph is GraphQL-native Headless CMS offers precise data retrieval, minimizing over-fetching and optimizing efficiency.

Examples

Look at some of the example projects to see Hygraph in action.

Why Hygraph

Choosing Hygraph for your PHP project

Using a GraphQL-native headless CMS with a PHP application streamlines development and empowers content editors. Developers benefit from GraphQL's efficient data retrieval, fetching only necessary and easily integrating with PHP's flexible backend.

This enhances performance and simplifies coding. Content editors enjoy a user-friendly interface to manage content without technical hurdles. Their updates via the CMS are seamlessly pushed to the live application, ensuring a smooth content flow and collaboration without disrupting the backend structure.

php cms

Developer Experience

We try to be the most un-opinionated CMS on the market with a wide collection of open source example projects to get you started.

Headless CMS

As a headless CMS (i.e. API based content management), you can be as modular and flexible as you need. We even support multiplatform content management.

Management API

Hygraph boasts a flexible and powerful management API to manage your content and schema, as well as a blazing fast content API.

Get started for free, or request a demo to discuss larger projects