Prerequisites
- Ensure you have a Microsoft Azure account and have set up an instance of Cognitive Services.
- Have a running Drupal site (preferably Drupal 9 or higher) with admin rights.
- Familiarity with Composer and Drush, which are required for module management in Drupal.
- Access to the terminal or command line interface on your server or development environment.
Install Microsoft Azure SDK for PHP
- Use Composer to install the Microsoft Azure SDK, which is required to connect with Azure Cognitive Services:
composer require microsoft/azure-storage-blob-php
- Add the installed package to your autoload.php if it hasn't been automatically included:
require 'vendor/autoload.php';
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
Enable Required Drupal Modules
- Install and enable the RESTful Web Services module. This may be already included with core Drupal installation.
- Install the Serialization module required for REST API support:
drush en serialization -y
- Enable the RESTful Web Services module:
drush en rest -y
Configure Azure Cognitive Services on Drupal
- Create a custom module if there isn't already a module in place for Azure services. Create a directory for your module:
mkdir web/modules/custom/azure_cognitive
- Create your .info.yml file to define your custom module:
name: 'Azure Cognitive Services'
type: module
description: 'Integration with Microsoft Azure Cognitive Services.'
core_version_requirement: ^8 || ^9
package: Custom
dependencies:
- drupal:rest
- drupal:serialization
- Use hook_rest_resource\_config() to configure the REST resource for Azure Cognitive Services:
/**
* Implements hook_rest_resource_config().
*/
function azure_cognitive_rest_resource_config() {
$resources = [];
$resources['cognitive_services'] = [
'uri_paths' => [
'canonical' => '/api/cognitive-services',
],
'methods' => ['GET', 'POST'],
'authentication_types' => ['basic_auth'],
];
return $resources;
}
Set Up Authentication Keys
- In the
azure\_cognitive.module
file, set up a function to authenticate using your Azure details.
/**
* Helper function to authenticate to Azure.
*/
function azure_cognitive_authenticate() {
$apiKey = 'YOUR_AZURE_API_KEY';
$endpoint = 'YOUR_AZURE_ENDPOINT';
$client = new \GuzzleHttp\Client();
$response = $client->post($endpoint, [
'headers' => [
'Ocp-Apim-Subscription-Key' => $apiKey,
],
]);
if ($response->getStatusCode() == 200) {
return json_decode($response->getBody()->getContents(), TRUE);
}
else {
drupal_set_message(t('Unable to authenticate with Azure.'), 'error');
}
}
Test the Integration
- Create a simple Drupal page or form that makes use of Azure Cognitive Services via the authenticated resource API:
function azure_cognitive_page() {
$result = azure_cognitive_authenticate();
// Example of using the result in your application logic.
if ($result) {
return [
'#markup' => t('Connected to Azure Cognitive Services!'),
];
}
return [
'#markup' => t('Connection failed!'),
];
}
Conclusion
- Ensure your API endpoints and authentication keys are kept secure and not exposed in your version-controlled files.
- Regularly check for updates on the SDK and Debian modules to maintain compatibility with Azure and Drupal updates.