

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 在使用 适用于 PHP 的 Amazon SDK 版本 3 的 Amazon SNS 中管理主题
<a name="sns-examples-managing-topics"></a>

要将通知发送到 Amazon Simple Queue Service (Amazon SQS)、HTTP/HTTPS URL、电子邮件、Amazon SMS 或 Amazon Lambda，您必须首先创建主题，以管理该主题的任何订阅用户的消息传送。

对于观察程序设计模式，此主题 (Topic) 类似于该主题 (Subject)。创建主题后，您可以添加在有消息发布到该主题时将自动通知的订阅者。

在[在使用 适用于 PHP 的 Amazon SDK 版本 3 的 Amazon SNS 中管理订阅](sns-examples-subscribing-unsubscribing-topics.md)中了解有关订阅主题的更多信息。

以下示例演示如何：
+ 使用 [CreateTopic](https://docs.amazonaws.cn/aws-sdk-php/v3/api/api-sns-2010-03-31.html#createtopic) 创建将通知发布到的主题。
+ 使用 [ListTopics](https://docs.amazonaws.cn/aws-sdk-php/v3/api/api-sns-2010-03-31.html#listtopic) 返回请求者的主题列表。
+ 使用 [DeleteTopic](https://docs.amazonaws.cn/aws-sdk-php/v3/api/api-sns-2010-03-31.html#deletetopic) 删除主题及其所有订阅。
+ 使用 [GetTopicAttributes](https://docs.amazonaws.cn/aws-sdk-php/v3/api/api-sns-2010-03-31.html#gettopicattributes) 返回主题的所有属性。
+ 使用 [SetTopicAttributes](https://docs.amazonaws.cn/aws-sdk-php/v3/api/api-sns-2010-03-31.html#settopicattributes) 允许主题所有者将主题的属性设置为新值。

有关使用 Amazon SNS 的更多信息，请参阅[用于消息传输状态的 Amazon SNS 主题属性](https://docs.amazonaws.cn/sns/latest/dg/sns-topic-attributes.html)。

适用于 PHP 的 Amazon SDKGitHub[ 上提供了](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code)的所有示例代码。

## 凭证
<a name="examplecredentials"></a>

运行示例代码之前，请配置您的 Amazon 凭证，如 [Amazon 使用 适用于 PHP 的 Amazon SDK 版本 3 进行身份验证](credentials.md) 中所述。然后导入 适用于 PHP 的 Amazon SDK，如 [安装 适用于 PHP 的 Amazon SDK 版本 3](getting-started_installation.md) 中所述。

## 创建主题
<a name="create-a-topic"></a>

要创建主题，请使用 [CreateTopic](https://docs.amazonaws.cn/sns/latest/api/API_CreateTopic.html) 操作。

您 Amazon Web Services 账户 中的各个主题名称必须唯一。

 **导入**。

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
use Aws\Sns\SnsClient;
```

 **示例代码** 

```
$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());
}
```

## 列出主题
<a name="list-your-topics"></a>

要列出当前 Amazon 区域中的最多 100 个现有主题，请使用 [ListTopics](https://docs.amazonaws.cn/sns/latest/api/API_ListTopics.html) 操作。

 **导入**。

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
use Aws\Sns\SnsClient;
```

 **示例代码** 

```
$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());
}
```

## 删除主题
<a name="delete-a-topic"></a>

要删除现有主题及其所有订阅，请使用 [DeleteTopic](https://docs.amazonaws.cn/sns/latest/api/API_DeleteTopic.html) 操作。

尚未传送至订阅者的任何消息也将删除。

 **导入**。

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
use Aws\Sns\SnsClient;
```

 **示例代码** 

```
$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());
}
```

## 获取主题属性
<a name="get-topic-attributes"></a>

要检索单个现有主题的属性，请使用 [GetTopicAttributes](https://docs.amazonaws.cn/sns/latest/api/API_GetTopicAttributes.html) 操作。

 **导入**。

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
use Aws\Sns\SnsClient;
```

 **示例代码** 

```
$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());
}
```

## 设置主题属性
<a name="set-topic-attributes"></a>

要更新单个现有主题的属性，请使用 [SetTopicAttributes](https://docs.amazonaws.cn/sns/latest/api/API_SetTopicAttributes.html) 操作。

您只能设置 `Policy`、`DisplayName` 和 `DeliveryPolicy` 属性。

 **导入**。

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
use Aws\Sns\SnsClient;
```

 **示例代码** 

```
$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());
}
```