PHP code example of contentful / contentful-management
1. Go to this page and download the library: Download contentful/contentful-management 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/ */
contentful / contentful-management example snippets
php
php
$client = new \Contentful\Management\Client('access-token');
php
$asset = new \Contentful\Management\Resource\Asset();
$file = new \Contentful\Core\File\RemoteUploadFile('Contentful.svg', 'image/svg+xml', $url);
$asset->setTitle('en-US', 'My asset')
->setDescription('en-US', 'My description')
->setFile('en-US', $file);
$environmentProxy->create($asset);
// Omit the locale to process the files for all locales
$asset->process('en-US');
$asset->setDescription('en-US', 'An even better description');
$asset->update();
$asset->archive();
$asset->unarchive();
$asset->publish();
$asset->unpublish();
$asset->delete();
php
$contentTypes = $environmentProxy->getContentTypes();
// Optionally, pass a query object
$query = (new \Contentful\Management\Query())
->setLimit(5);
$contentTypes = $environmentProxy->getContentTypes($query);
$contentType = $environmentProxy->getContentType($contentTypeId);
echo $contentType->getSystemProperties()->getId();
echo $contentType->getName();
// Fetch the published version of content types
$contentTypes = $environmentProxy->getPublishedContentTypes($query);
$contentType = $environmentProxy->getPublishedContentType($contentTypeId);
// Fetch snapshots from a content type, or from an environment proxy
$snapshots = $contentType->getSnapshots();
$snapshot = $contentTy->getSnapshot($snapshotId);
$snapshots = $environmentProxy->getContentTypeSnapshots($contentTypeId);
$snapshot = $environmentProxy->getContentTypeSnapshot($contentTypeId, $snapshotId);
php
$readOnly = false;
$personalAccessToken = new \Contentful\Management\Resource\PersonalAccessToken('Development access token', $readOnly);
$client->create($personalAccessToken);
// For security reasons, the actual token will only be available after creation.
echo $personalAccessToken->getToken();
$personalAccessToken->revoke();
php
$role = new \Contentful\Management\Resource\Role('Publisher');
$policy = new \Contentful\Management\Resource\Policy('allow', 'publish');
$role->addPolicy($policy);
$constraint = new \Contentful\Management\Resource\Role\Constraint\AndConstraint([
new \Contentful\Management\Resource\Role\Constraint\EqualityConstraint('sys.type', 'Entry'),
new \Contentful\Management\Resource\Role\Constraint\EqualityConstraint('sys.type', 'Asset'),
]);
$policy->setConstraint($constraint);
$spaceProxy->create($role);
$policy->delete();
php
// You can pass as argument an fopen resource, an actual string, or a PSR-7 compatible stream
$upload = new \Contentful\Management\Resource\Upload(\fopen($myFile, 'r'));
$spaceProxy->create($upload);
$asset = new \Contentful\Management\Resource\Asset();
// To use the upload as an asset, you need to supply an asset name and a mime type
$asset->setFile('en-US', $upload->asAssetFile('my-asset-name.png', 'image/png'));
$environmentProxy->create($asset);
$asset->process();
$upload->delete();
php
$client = new \Contentful\Management\Client('KEY',['max_rate_limit_retries' => 2]);
$proxy = $client->getSpaceProxy('SPACE_ID');
$envName = uniqid();
$env = new \Contentful\Management\Resource\Environment($envName);
$proxy->create($env); //this call will retry two times (so three calls couting the original one), before throwing an exception
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.