Managing topics in Amazon SNS with the Amazon SDK for PHP Version 3 - Amazon SDK for PHP
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Managing topics in Amazon SNS with the Amazon SDK for PHP Version 3

To send notifications to Amazon Simple Queue Service (Amazon SQS), HTTP/HTTPS URLs, email, Amazon SMS, or Amazon Lambda, you must first create a topic that manages the delivery of messages to any subscribers of that topic.

In terms of the observer design pattern, a topic is like the subject. After a topic is created, you add subscribers that are notified automatically when a message is published to the topic.

Learn more about subscribing to topics in Managing Subscriptions in Amazon SNS with Amazon SDK for PHP Version 3.

The following examples show how to:

  • Create a topic to publish notifications to using CreateTopic.

  • Return a list of the requester’s topics using ListTopics.

  • Delete a topic and all of its subscriptions using DeleteTopic.

  • Return all of the properties of a topic using GetTopicAttributes.

  • Allow a topic owner to set an attribute of the topic to a new value using SetTopicAttributes.

For more information about using Amazon SNS, see Amazon SNS Topic Attributes for Message Delivery Status.

All the example code for the Amazon SDK for PHP is available here on GitHub.

Credentials

Before running the example code, configure your Amazon credentials, as described in Credentials. Then import the Amazon SDK for PHP, as described in Basic usage.

Create a topic

To create a topic, use the CreateTopic operation.

Each topic name in your Amazon Web Services account must be unique.

Imports

require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Sns\SnsClient;

Sample Code

$SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-03-31' ]); $topicname = 'myTopic'; try { $result = $SnSclient->createTopic([ 'Name' => $topicname, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

List your topics

To list up to 100 existing topics in the current Amazon Region, use the ListTopics operation.

Imports

require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Sns\SnsClient;

Sample Code

$SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-03-31' ]); try { $result = $SnSclient->listTopics(); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

Delete a topic

To remove an existing topic and all of its subscriptions, use the DeleteTopic operation.

Any messages that have not been delivered yet to subscribers will also be deleted.

Imports

require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Sns\SnsClient;

Sample Code

$SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-03-31' ]); $topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic'; try { $result = $SnSclient->deleteTopic([ 'TopicArn' => $topic, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

Get topic attributes

To retrieve properties of a single existing topic, use the GetTopicAttributes operation.

Imports

require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Sns\SnsClient;

Sample Code

$SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-03-31' ]); $topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic'; try { $result = $SnSclient->getTopicAttributes([ 'TopicArn' => $topic, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

Set topic attributes

To update properties of a single existing topic, use the SetTopicAttributes operation.

You can set only the Policy, DisplayName, and DeliveryPolicy attributes.

Imports

require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Sns\SnsClient;

Sample Code

$SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-03-31' ]); $attribute = 'Policy | DisplayName | DeliveryPolicy'; $value = 'First Topic'; $topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic'; try { $result = $SnSclient->setTopicAttributes([ 'AttributeName' => $attribute, 'AttributeValue' => $value, 'TopicArn' => $topic, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }