Back

Step by Step Guide to Building an Adobe Experience Manager API Integration in PHP

Aug 3, 20246 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of Adobe Experience Manager (AEM) API integration? You're in for a treat. AEM's API is a powerful tool that'll let you interact with content, assets, and more. In this guide, we'll walk through creating a robust PHP integration that'll have you manipulating AEM like a pro in no time.

Prerequisites

Before we jump in, make sure you've got:

  • A PHP environment up and running
  • Access to an AEM instance
  • Your AEM API credentials handy

Got all that? Great! Let's get cooking.

Setting up the Development Environment

First things first, let's get our PHP environment primed for AEM integration:

composer require guzzlehttp/guzzle

This'll install Guzzle, our HTTP client of choice for making API requests.

Basic API Connection

Now, let's establish a connection to AEM:

<?php require 'vendor/autoload.php'; use GuzzleHttp\Client; $client = new Client([ 'base_uri' => 'https://your-aem-instance.com', 'auth' => ['username', 'password'] ]);

Simple, right? We're using basic auth here, but feel free to level up with OAuth if that's your jam.

Performing CRUD Operations

Let's get our hands dirty with some CRUD operations:

Create Content

$response = $client->post('/content/mysite/en.json', [ 'json' => [ 'jcr:primaryType' => 'cq:Page', 'jcr:content' => [ 'jcr:primaryType' => 'cq:PageContent', 'jcr:title' => 'My New Page', 'sling:resourceType' => 'mysite/components/page' ] ] ]);

Read Content

$response = $client->get('/content/mysite/en.json'); $content = json_decode($response->getBody(), true);

Update Content

$response = $client->post('/content/mysite/en.json', [ 'json' => [ 'jcr:content' => [ 'jcr:title' => 'Updated Page Title' ] ] ]);

Delete Content

$response = $client->delete('/content/mysite/en.json');

Working with Assets

AEM's not just about pages. Let's handle some assets:

// Upload an asset $response = $client->post('/content/dam/mysite.createasset.html', [ 'multipart' => [ [ 'name' => 'file', 'contents' => fopen('path/to/image.jpg', 'r') ] ] ]); // Get asset metadata $response = $client->get('/content/dam/mysite/image.jpg.json'); $metadata = json_decode($response->getBody(), true);

Content Management

Creating pages and working with components is a breeze:

// Create a new page $response = $client->post('/content/mysite/en.html', [ 'form_params' => [ 'cmd' => 'createPage', 'parentPath' => '/content/mysite/en', 'title' => 'New Page', 'template' => '/apps/mysite/templates/contentpage' ] ]); // Add a component to a page $response = $client->post('/content/mysite/en/new-page/jcr:content.json', [ 'json' => [ 'sling:resourceType' => 'mysite/components/text', 'text' => '<p>Hello, AEM!</p>' ] ]);

Querying Content

Need to find specific content? QueryBuilder's got your back:

$response = $client->get('/bin/querybuilder.json', [ 'query' => [ 'path' => '/content/mysite', 'type' => 'cq:Page', 'property' => 'jcr:content/sling:resourceType', 'property.value' => 'mysite/components/page' ] ]); $results = json_decode($response->getBody(), true);

Error Handling and Logging

Don't forget to catch those errors and log important info:

try { $response = $client->get('/nonexistent-path.json'); } catch (\GuzzleHttp\Exception\ClientException $e) { error_log('AEM API Error: ' . $e->getMessage()); }

Best Practices

A few tips to keep your integration smooth:

  • Implement rate limiting to avoid overwhelming the AEM instance
  • Cache responses when possible to reduce API calls
  • Always sanitize and validate input before sending it to AEM

Testing the Integration

Last but not least, test your integration thoroughly:

public function testCreatePage() { // Your test code here }

Conclusion

And there you have it! You're now equipped to build a robust AEM API integration in PHP. Remember, this is just the tip of the iceberg. AEM's API is vast and powerful, so don't be afraid to explore and experiment.

Happy coding, and may your AEM adventures be bug-free and blazing fast!