A PHP client for interacting with DatoCMS's GraphQL API. Perfect for building PHP applications with content managed in DatoCMS.
Features:
- ✅ CRUD operations
- 🖼 File uploads
- 🔄 Automatic retry logic
- 🛡️ Robust error handling
- 📦 Composer ready
-
Require via Composer:
composer require nia-cloud-official/datocms-php-sdk
-
Get API token:
- Go to DatoCMS → Settings → API Tokens
- Copy Read-only API token
<?php
require 'vendor/autoload.php';
use DatoCMS\Client;
$client = new Client('DATOCMS_API_TOKEN');
try {
// Get all blog posts
$result = $client->query('
query {
allPosts {
title
content
}
}
');
print_r($result);
} catch (Exception $e) {
die("Error: " . $e->getMessage());
}
$result = $client->query('
query GetPost($id: ID!) {
post(filter: {id: {eq: $id}}) {
title
content
}
}
', ['id' => '123']);
$client->query('
mutation CreatePost($title: String!) {
createPost(title: $title) {
id
}
}
', ['title' => 'New Post']);
$file = $client->uploadFile('/path/to/image.jpg');
echo "Uploaded to: " . $file['url'];
try {
// Your code here
} catch (DatoCMS\Exceptions\AuthException $e) {
die("Invalid API token! 💀");
} catch (DatoCMS\Exceptions\RateLimitException $e) {
sleep(60); // Wait 1 minute
} catch (Exception $e) {
error_log($e->getMessage());
}
- Cache responses:
$client = new Client(getenv('DATOCMS_API_TOKEN'), [ 'cache' => new RedisCache() ]);
- Use query variables
- Validate inputs before sending
Error | Solution |
---|---|
401 Unauthorized |
Check .env file |
429 Too Many Requests |
Implement retry logic |
File upload failed |
Check file permissions |
Unexpected response |
Validate GraphQL query |
- Fork the repository
- Create your feature branch:
git checkout -b feature/amazing-feature
- Commit changes:
git commit -m 'Add amazing feature'
- Push to branch:
git push origin feature/amazing-feature
- Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Need Help?
Email: miltonhyndrex@gmail.com
Official Docs: DatoCMS Documentation