php-awsHow do I use AWS SNS to publish a message using PHP?
To use AWS SNS to publish a message using PHP, you must first install the AWS SDK for PHP. You can then use the following code to publish a message:
<?php
// Include the SDK using the Composer autoloader
require 'vendor/autoload.php';
// Create an SNS client
$sns = new Aws\Sns\SnsClient([
'region' => 'us-east-1',
'version' => 'latest'
]);
// Publish a message to an SNS topic
$result = $sns->publish([
'Message' => 'Hello world!',
'TopicArn' => 'arn:aws:sns:us-east-1:123456789012:MyTopic',
]);
echo $result['MessageId'];
This code will produce an output of the MessageId
of the published message.
Code explanation
require 'vendor/autoload.php';
: This line includes the AWS SDK for PHP.$sns = new Aws\Sns\SnsClient([...])
: This line creates an SNS client.$result = $sns->publish([...])
: This line publishes a message to an SNS topic.echo $result['MessageId'];
: This line prints out theMessageId
of the published message.
For more information, please refer to the following links:
More of Php Aws
- How can I use PHP to connect to an Amazon Aurora database?
- How do I use PHP to create a ZIP file on AWS?
- How can I use AWS and Zksync together with PHP?
- How can I use Yum to install PHP on an Amazon EC2 instance?
- How can I use an AWS SQS Worker with PHP?
- How can I use AWS WAF to secure my PHP application?
- How can I use AWS PHP SDK without credentials?
- How do I generate an AWS Signature Version 4 with PHP?
- How do I determine the version of PHP I am running on AWS?
- How can I use the AWS S3 S3Client library with PHP?
See more codes...