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.
Before we jump in, make sure you've got:
Got all that? Great! Let's get cooking.
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.
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.
Let's get our hands dirty with some CRUD operations:
$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' ] ] ]);
$response = $client->get('/content/mysite/en.json'); $content = json_decode($response->getBody(), true);
$response = $client->post('/content/mysite/en.json', [ 'json' => [ 'jcr:content' => [ 'jcr:title' => 'Updated Page Title' ] ] ]);
$response = $client->delete('/content/mysite/en.json');
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);
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>' ] ]);
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);
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()); }
A few tips to keep your integration smooth:
Last but not least, test your integration thoroughly:
public function testCreatePage() { // Your test code here }
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!