Version: 1.0
Author: Milton Vafana
License: MIT
- Overview
- Installation
- Configuration
- Basic Usage
- Advanced Features
- Examples
- Troubleshooting
- Best Practices
- Support
A PHP SDK to interact with DatoCMS's GraphQL API. Designed for:
- Building PHP applications with content managed in DatoCMS
- Migrating content between environments
- Creating custom integrations
Key Features:
- ✅ Simple data fetching
- 🖼 File/asset uploads
- 🔄 Automatic retry logic
- 🛡️ Robust error handling
Skip if already installed:
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
mkdir my-datocms-app
cd my-datocms-app
composer require nia-cloud-official/datocms-php-sdk
- Log in to DatoCMS
- Go to Settings > API Tokens
- Copy Read-only API token
touch .env
Add your token:
# .env
DATOCMS_API_TOKEN=your_token_here
Create app.php
:
<?php
require 'vendor/autoload.php';
use DatoCMS\Client;
// Load environment variables
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
// Initialize client
$client = new Client('DATOCMS_API_TOKEN');
Example: Get blog posts
try {
$result = $client->query('
query {
allPosts {
id
title
content
}
}
');
foreach ($result['allPosts'] as $post) {
echo "Title: {$post['title']}\n";
}
} catch (Exception $e) {
die("Error: " . $e->getMessage());
}
$mutation = '
mutation CreatePost($title: String!, $content: String!) {
createPost(
title: $title
content: $content
) {
id
}
}
';
$variables = [
'title' => 'New Post',
'content' => 'Hello World!'
];
$result = $client->query($mutation, $variables);
$mutation = '
mutation UpdatePost($id: ID!, $title: String!) {
updatePost(
id: $id
title: $title
) {
id
title
}
}
';
$variables = [
'id' => '123',
'title' => 'Updated Title'
];
$mutation = '
mutation DeletePost($id: ID!) {
deletePost(id: $id) {
id
}
}
';
$variables = ['id' => '123'];
try {
$file = $client->uploadFile('/path/to/image.jpg');
echo "Uploaded file URL: {$file['url']}";
} catch (ApiException $e) {
die("Upload failed: " . $e->getMessage());
}
Handle specific errors:
try {
// Your code here
} catch (AuthException $e) {
die("❌ Invalid API token! Check your .env file");
} catch (RateLimitException $e) {
sleep(60); // Wait 1 minute
retry();
} catch (ApiException $e) {
error_log($e->getMessage());
die("A wild error appeared! 🐞");
}
// Get latest 5 posts with images
$result = $client->query('
query {
allPosts(orderBy: [date_DESC], first: 5) {
title
excerpt
coverImage {
url
alt
}
}
}
');
foreach ($result['allPosts'] as $post) {
echo "<article>
<img src='{$post['coverImage']['url']}' alt='{$post['coverImage']['alt']}'>
<h2>{$post['title']}</h2>
<p>{$post['excerpt']}</p>
</article>";
}
$mutation = '
mutation UpdateUser($id: ID!, $bio: String!) {
updateUser(id: $id, bio: $bio) {
id
bio
}
}
';
$client->query($mutation, [
'id' => 'user-456',
'bio' => 'PHP developer passionate about CMS integrations'
]);
Error | Solution |
---|---|
401 Unauthorized |
1. Check .env file2. Verify token in DatoCMS dashboard |
Could not resolve host |
Check internet connection |
File upload failed |
1. Verify file exists 2. Check file permissions |
Unexpected JSON error |
Validate your GraphQL query |
- Never commit
.env
Add to.gitignore
:echo ".env" >> .gitignore
- Use query variables
Avoid string interpolation in GraphQL queries - Implement caching
Example with Redis:$client = new Client($_ENV['DATOCMS_API_TOKEN'], [ 'cache' => new RedisCache() ]);
- Validate inputs
Sanitize data before sending to DatoCMS
Need Help?
Contact: miltonhyndrex@gmail.com
Official Resources: