1. Go to this page and download the library: Download dealnews/datocms-cma-client library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dealnews / datocms-cma-client example snippets
use DealNews\DatoCMS\CMA\Client;
$client = new Client('<your-api-token>', '<your-env-name>');
$list = $client->record->list();
use DealNews\DatoCMS\CMA\Parameters\Record;
$parameters = new Record();
$parameters->filter->ids = ['abcd', 'efgh'];
$list = $client->record->list($parameters);
use DealNews\DatoCMS\CMA\Input\Record;
use DealNews\DatoCMS\CMA\DataTypes\Scalar;
use DealNews\DatoCMS\CMA\DataTypes\Color;
use DealNews\DatoCMS\CMA\DataTypes\Location;
use DealNews\DatoCMS\CMA\DataTypes\Asset;
use DealNews\DatoCMS\CMA\DataTypes\SEO;
// Create a new record with the item_type_id
$record = new Record('DxMaW10UQiCmZcuuA-IkkA');
// Use Scalar DataType for simple values
$title = Scalar::init()->set('Hello World');
$record->attributes['title'] = $title;
// Or use DataType helper methods for complex types
$color = Color::init()->setColor(255, 128, 64, 200);
$record->attributes['brand_color'] = $color;
$location = Location::init()->setLocation(40.7128, -74.0060);
$record->attributes['coordinates'] = $location;
$asset = Asset::init()->setAsset('upload_123', 'Image Title', 'Alt Text');
$record->attributes['image'] = $asset;
$seo = SEO::init()->setSEO('Page Title', 'Page Description', 'image_id', 'summary', false);
$record->attributes['seo'] = $seo;
// Convert to array and create the record
$result = $client->record->create($record->toArray());
use DealNews\DatoCMS\CMA\Input\Record;
$record = new Record('DxMaW10UQiCmZcuuA-IkkA');
$record->attributes = [
'title' => 'Hello World',
'body' => 'This is the body',
'brand_color' => [
'red' => 255,
'green' => 128,
'blue' => 64,
'alpha' => 200
]
];
$result = $client->record->create($record->toArray());
use DealNews\DatoCMS\CMA\Input\Model;
$model = new Model();
$model->attributes['name'] = 'Updated Blog Post';
$result = $client->model->update('model-id', $model);
// Upload from URL
$upload = $client->upload->uploadFromUrl('https://example.com/image.jpg');
// With custom filename and metadata
$upload = $client->upload->uploadFromUrl(
'https://example.com/image.jpg',
'custom-filename.jpg',
['author' => 'Jane Doe']
);
use DealNews\DatoCMS\CMA\Parameters\Upload;
$params = new Upload();
$params->filter->type = 'image'; // Filter by type: image, video, audio, etc.
$params->filter->query = 'banner'; // Full-text search
$params->filter->tags = ['hero', 'featured']; // Filter by tags
$params->filter->upload_collection_id = 'collection-123'; // Filter by folder
$params->order_by->addOrderByField('created_at', 'DESC');
$params->page->limit = 25;
$uploads = $client->upload->list($params);
use DealNews\DatoCMS\CMA\Input\UploadCollection;
// List collections
$collections = $client->upload_collection->list();
// Create a collection
$collection = new UploadCollection();
$collection->attributes['label'] = 'Product Images';
$result = $client->upload_collection->create($collection);
// Create a nested collection
$subcollection = new UploadCollection();
$subcollection->attributes['label'] = 'Thumbnails';
$subcollection->parent_id = $result['data']['id'];
$client->upload_collection->create($subcollection);
// Delete a collection
$client->upload_collection->delete($collection_id);
// List all tags
$tags = $client->upload_tag->list();
// Create a tag
$tag = $client->upload_tag->create('featured');
// Delete a tag
$client->upload_tag->delete($tag['data']['id']);