PHP code example of dealnews / datocms-cma-client

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());

$new_record = [
    'type' => 'item',
    'attributes' => [
        'title' => 'Hello World',
        'body' => 'This is the body'
    ],
    'relationships' => [
        'item_type' => [
            'data' => [
                'type' => 'item_type',
                'id' => 'DxMaW10UQiCmZcuuA-IkkA'
            ],
        ]       
    ]
];

$result = $client->record->create($new_record);

$models = $client->model->list();

$model = $client->model->retrieve('model-id');

use DealNews\DatoCMS\CMA\Input\Model;

$model = new Model();
$model->attributes['name'] = 'Blog Post';
$model->attributes['api_key'] = 'blog_post';
$model->attributes['singleton'] = false;
$model->attributes['sortable'] = true;
$model->attributes['draft_mode_active'] = true;

$result = $client->model->create($model);

use DealNews\DatoCMS\CMA\Input\Model;

$model = new Model();
$model->attributes['name'] = 'Updated Blog Post';

$result = $client->model->update('model-id', $model);

$result = $client->model->delete('model-id');

$result = $client->model->duplicate('model-id');

// Upload a local file
$upload = $client->upload->uploadFile('/path/to/image.jpg');

// Upload with metadata
$upload = $client->upload->uploadFile('/path/to/image.jpg', [
    'author'    => 'John Doe',
    'copyright' => '© 2025 Company',
    'notes'     => 'Internal notes about this file',
    'tags'      => ['banner', 'hero', 'featured'],
    'default_field_metadata' => [
        'en' => ['alt' => 'Hero banner image', 'title' => 'Main Banner'],
        'es' => ['alt' => 'Imagen de banner', 'title' => 'Banner Principal'],
    ],
]);

// Upload to a specific collection (folder)
$upload = $client->upload->uploadFile('/path/to/image.jpg', null, 'collection-id');

// 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']);

$smart_tags = $client->upload_smart_tag->list();

// Bulk delete uploads
$client->upload->deleteBulk(['upload-1', 'upload-2', 'upload-3']);

// Bulk update metadata
$client->upload->updateBulk(
    ['upload-1', 'upload-2'],
    ['author' => 'Updated Author', 'copyright' => '© 2025']
);

use DealNews\DatoCMS\CMA\Input\Upload;

// Step 1: Request upload permission
$request = $client->upload_request->create('image.jpg');
$s3_url = $request['data']['attributes']['url'];
$s3_headers = $request['data']['attributes']['request_headers'];

// Step 2: Upload to S3 (using your own HTTP client)
// PUT $s3_url with file contents and $s3_headers

// Step 3: Register the upload in DatoCMS
$upload = new Upload();
$upload->attributes->path = $request['data']['id'];
$upload->attributes->author = 'John Doe';
$result = $client->upload->create($upload);