

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用第 3 適用於 PHP 的 AWS SDK 版管理 Amazon SNS 中的主題
<a name="sns-examples-managing-topics"></a>

若要傳送通知至 Amazon Simple Queue Service (Amazon SQS)、HTTP/HTTPS URLs AWS SMS、電子郵件或 AWS Lambda，您必須先建立主題來管理訊息交付給該主題的任何訂閱者。

就觀察者設計模式而言，主題即有如主旨。建立主題之後，您便要新增訂閱者，其將於訊息發佈至該主題時自動收到通知。

進一步了解如何使用第 [3 適用於 PHP 的 AWS SDK 版在 Amazon SNS 中管理訂閱](sns-examples-subscribing-unsubscribing-topics.md)中的主題。

下列範例示範如何：
+ 使用 [CreateTopic](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sns-2010-03-31.html#createtopic) 建立要發佈通知的主題。
+ 使用 [ListTopics](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sns-2010-03-31.html#listtopic) 傳回申請者的主題清單。
+ 使用 [DeleteTopic](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sns-2010-03-31.html#deletetopic) 刪除主題及其所有訂閱。
+ 使用 [GetTopicAttributes](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sns-2010-03-31.html#gettopicattributes) 傳回主題的所有屬性。
+ 使用 [SetTopicAttributes](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sns-2010-03-31.html#settopicattributes) 允許主題擁有者將主題的屬性設為新的值。

如需使用 Amazon SNS 的詳細資訊，請參閱[訊息傳遞狀態的 Amazon SNS 主題屬性](https://docs.aws.amazon.com/sns/latest/dg/sns-topic-attributes.html)。

您可以在 GitHub 上 適用於 PHP 的 AWS SDK 取得 的所有範例程式碼。 [ GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code)

## 登入資料
<a name="examplecredentials"></a>

在執行範例程式碼之前，請先設定您的 AWS 登入資料，如中所述[AWS 使用第 3 適用於 PHP 的 AWS SDK 版向 驗證](credentials.md)。然後匯入 適用於 PHP 的 AWS SDK，如 中所述[安裝第 3 適用於 PHP 的 AWS SDK 版](getting-started_installation.md)。

## 建立主題
<a name="create-a-topic"></a>

若要建立主題，請使用 [CreateTopic](https://docs.aws.amazon.com/sns/latest/api/API_CreateTopic.html) 操作。

您 中的每個主題名稱 AWS 帳戶 都必須是唯一的。

 **匯入** 

```
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>

若要列出目前 AWS 區域中最多 100 個現有主題，請使用 [ListTopics](https://docs.aws.amazon.com/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.aws.amazon.com/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.aws.amazon.com/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.aws.amazon.com/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());
}
```