PHP code example of rogervila / cosmicjs-php

1. Go to this page and download the library: Download rogervila/cosmicjs-php 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/ */

    

rogervila / cosmicjs-php example snippets


// Set the configuration
$config = new \Rogervila\CosmicJS\Config();

$config
    ->setBucketSlug('YOUR_BUCKET_SLUG')
    ->setWriteKey('YOUR_WRITE_KEY')
    ->setReadKey('YOUR_READ_KEY');

// Create an instance
$cosmic = new \Rogervila\CosmicJS\CosmicJS($config);

// Bucket information
$cosmic->getBucket();

// Read objects
$cosmic->getObjects();

// Read object by slug
$cosmic->getObject('OBJECT_SLUG');

// Read media
$cosmic->getMedia();

// Create an object

// object parameters
$parameters = new \Rogervila\CosmicJS\Parameters($config);
$parameters->typeSlug = 'my-object';
$parameters->title = 'My Object';

// add metafields
$meta = new \Rogervila\CosmicJS\Metafield();

$meta->key = 'field-name';
$meta->type = 'textarea';
$meta->value = 'lorem ipsum';

$parameters->metafields = [
    $meta,
];

$cosmic->addObject($parameters);

// Edit an object

// create object parameters...

$cosmic->editObject($parameters);


// Delete an object

// create object parameters...

$cosmic->editObject($parameters);

shell
$ composer