---
# System prepended metadata

title: Chapter 21.SNS
tags: [大話AWS雲端架構]

---

###### tags: `大話AWS雲端架構`

# Chapter 21.SNS

## 概念
1. 推播通知用戶 SNS
2. 核心組件
    - Publisher 推送者 
    - Topic 主題
    - Subscriber 訂閱者
3. 跳過 SNS+SQS
4. 跳過 考題解析
5. 架構圖
6. 名詞 (看第二點)
7. 小結
    就跟youbute訂閱機制差不多

## AWS設定

- 新增主題
    - https://us-east-2.console.aws.amazon.com/sns/v3/home?region=us-east-2#/topics
- 訂閱主題
    - Email
    - 簡訊
    - HTTP
    - 其他


## [實作](https://docs.aws.amazon.com/zh_tw/sdk-for-php/v3/developer-guide/sns-examples-subscribing-unsubscribing-topics.html)

- 安裝 
```
composer require aws/aws-sdk-php
```

- CODE 
```php=

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

    test();
    function test(){
        $credentials = new Aws\Credentials\Credentials('AKIA2IHISSKQTHZXLLF2' ,             'c7eJ6821bvROcQ/YrpqK61T6/opzkRskK+/AgyeD');

        $SnSclient = new SnsClient([
            // 'profile' => 'default',
            'region' => 'us-east-2',
            'version' => '2010-03-31',
            'credentials' => $credentials
        ]);
        
        $message = 'This message is sent from a Amazon SNS code sample.';
        $topic = 'arn:aws:sns:us-east-2:704862392993:edwardTopic';
        
        try {
            $result = $SnSclient->publish([
                'Message' => $message,
                'TopicArn' => $topic,
            ]);
            var_dump($result);
        } catch (AwsException $e) {
            // output error message if fails
            error_log($e->getMessage());
        } 
    }

?>
```

