9951 explained code solutions for 126 technologies


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

  1. require 'vendor/autoload.php';: This line includes the AWS SDK for PHP.
  2. $sns = new Aws\Sns\SnsClient([...]): This line creates an SNS client.
  3. $result = $sns->publish([...]): This line publishes a message to an SNS topic.
  4. echo $result['MessageId'];: This line prints out the MessageId of the published message.

For more information, please refer to the following links:

Edit this code on GitHub