contentful/core
Core components shared by Contentful’s PHP Delivery and Management SDKs. Provides foundational utilities and abstractions for interacting with Contentful APIs. Requires PHP 8.0+. Intended for internal SDK use, not for direct third‑party consumption.
Installation
composer require contentful/core
Note: This package is foundational—use it only with contentful/php or contentful-management.php.
First Use Case
Initialize a Client (example for Delivery API):
use Contentful\Client;
use Contentful\Delivery\Client as DeliveryClient;
$client = new Client([
'space' => 'your-space-id',
'accessToken' => 'your-access-token',
'host' => 'cdn.contentful.com',
]);
$deliveryClient = new DeliveryClient($client);
Key Classes to Explore
Contentful\Client: Base client for API calls.Contentful\Delivery\Client: For content delivery operations.Contentful\Management\Client: For managing spaces, content models, etc.Contentful\Resource\ResourceCollection: Handles paginated responses.Content Delivery Fetch entries, assets, or locales:
$entries = $deliveryClient->getEntries(['content_type' => 'blogPost']);
foreach ($entries as $entry) {
echo $entry->getField('title');
}
Management Operations Create/update content models or entries:
$managementClient = new ManagementClient($client);
$space = $managementClient->getSpace('your-space-id');
$contentType = $space->getContentType('blogPost');
$contentType->setField('title', ['name' => 'Title', 'validations' => []]);
$contentType->save();
Pagination Handling
Use ResourceCollection for iterating over large datasets:
$collection = $deliveryClient->getEntries(['limit' => 100]);
foreach ($collection as $entry) {
// Process entry
}
// Auto-fetches next page if needed
Error Handling Wrap API calls in try-catch:
try {
$deliveryClient->getEntry('blogPost', '123');
} catch (\Contentful\Exception\ContentfulException $e) {
\Log::error($e->getMessage());
}
Laravel Service Provider Bind clients to the container for dependency injection:
$this->app->singleton(DeliveryClient::class, function ($app) {
return new DeliveryClient(new Client(config('contentful.delivery')));
});
Caching Responses
Cache ResourceCollection results for performance:
$entries = Cache::remember('contentful_blog_posts', now()->addHours(1), function () {
return $deliveryClient->getEntries(['content_type' => 'blogPost']);
});
Environment Configuration
Store credentials in .env:
CONTENTFUL_SPACE_ID=your-space-id
CONTENTFUL_DELIVERY_TOKEN=your-token
CONTENTFUL_MANAGEMENT_TOKEN=your-management-token
Direct Usage Warning
contentful/php or contentful-management.php for full functionality.Rate Limiting
429 Too Many Requests gracefully:
if ($e->getCode() === 429) {
sleep($e->getRetryAfter());
retry();
}
Field Access
getField() or array notation:
$title = $entry->getField('title'); // Preferred
// or
$title = $entry['fields']['title'];
Locale Handling
$deliveryClient->getEntries(['content_type' => 'blogPost', 'locale' => 'en-US']);
Enable Debug Mode
$client = new Client([
'debug' => true, // Logs HTTP requests/responses
// ...
]);
Common HTTP Errors
401: Invalid token or space ID.404: Resource not found (e.g., entry/content type).403: Permission denied (check management token scopes).Custom HTTP Client Override the default Guzzle client:
use Contentful\Http\Client as HttpClient;
use GuzzleHttp\Client as GuzzleClient;
$httpClient = new HttpClient(new GuzzleClient([
'timeout' => 30,
'headers' => ['User-Agent' => 'MyApp/1.0'],
]));
$client = new Client(['httpClient' => $httpClient]);
Middleware Add request/response middleware:
$client->getHttpClient()->getEmitter()->addSubscriber(new class {
public function process(\Psr\Http\Message\RequestInterface $request, callable $next) {
$request = $request->withHeader('X-Custom-Header', 'value');
return $next($request);
}
});
Event Listeners
Listen for API events (e.g., Contentful\Events\RequestSent):
$client->getHttpClient()->getEmitter()->addListener(
'Contentful\Events\RequestSent',
function ($event) {
\Log::debug('Request sent to:', [$event->getRequest()->getUri()]);
}
);
Host Overrides
Use host config to switch between environments (e.g., preview API):
$client = new Client(['host' => 'preview.contentful.com']);
SSL Verification Disable for self-signed certs (not recommended for production):
$client = new Client(['verify' => false]);
How can I help you explore Laravel packages today?