Here’s a basic example of how to connect to DynamoDB and retrieve data using the AWS SDK for PHP in Laravel:
composer require aws/aws-sdk-php
In your Laravel application, create an AWS service client for DynamoDB:Install the AWS SDK for PHP via Composer by running the following command:
use Aws\DynamoDb\DynamoDbClient;
$client = new DynamoDbClient([
'region' => 'us-west-2',
'version' => '2012-08-10',
'credentials' => [
'key' => 'YOUR_AWS_ACCESS_KEY_ID',
'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
],
]);
Use the client to retrieve data from your DynamoDB table:
use Aws\DynamoDb\Exception\DynamoDbException;
try {
$result = $client->getItem([
'TableName' => 'YOUR_TABLE_NAME',
'Key' => [
'KEY_NAME' => [
'S' => 'KEY_VALUE',
],
],
]);
print_r($result['Item']);
} catch (DynamoDbException $e) {
echo "Unable to retrieve item:\n";
echo $e->getMessage() . "\n";
}
This is just a basic example. You can refer to the official AWS SDK for PHP documentation for more information and options: https://docs.aws.amazon.com/en_us/sdk-for-php/v3/developer-guide/getting-started.html