

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

# 使用第 3 適用於 PHP 的 AWS SDK 版在 Amazon SNS 中傳送簡訊
<a name="sns-examples-sending-sms"></a>

您可以使用 Amazon Simple Notification Service (Amazon SNS) 將文字訊息或簡訊傳送至啟用 SMS 的裝置。您可以直接傳送訊息至一組電話號碼，或一次傳送一則訊息至多組電話號碼，只要訂閱那些電話號碼到主題並且傳送您的訊息到該主題即可。

使用 Amazon SNS 指定簡訊的偏好設定，例如如何最佳化交付 （用於成本或可靠交付）、每月花費限制、如何記錄訊息交付，以及是否訂閱每日簡訊用量報告。系統會擷取這些偏好設定，並將其設定為 Amazon SNS 的 SMS 屬性。

當您傳送簡訊時，請指定使用 E.164 格式的電話號碼。E.164 是國際電信通訊所採用的電話號碼結構的標準。遵循此格式的電話號碼最多可以有 15 位數，前面加上加號 (\$1) 字元和國碼。例如，E.164 格式的美國電話號碼顯示為 \$11001XXX5550100。

下列範例示範如何：
+ 使用 [GetSMSAttributes](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sns-2010-03-31.html#getsmsattributes) 擷取從您的帳戶傳送簡訊的預設設定。
+ 使用 [SetSMSAttributes](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sns-2010-03-31.html#setsmsattributes) 更新從您的帳戶傳送簡訊的預設設定。
+ 使用 [CheckIfPhoneNumberISOptedOut](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sns-2010-03-31.html#checkifphonenumberisoptedout) 檢查指定的電話號碼擁有者是否已選擇停止從您的帳戶接收簡訊。
+ 使用 [ListPhoneNumberOptedOut](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sns-2010-03-31.html#listphonenumbersoptedout) 列出其擁有者已選擇停止從您的帳戶接收簡訊的電話號碼。
+ 使用 [Publish](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sns-2010-03-31.html#publish) 直接傳送文字訊息 (簡訊) 至電話號碼。

如需使用 Amazon SNS 的詳細資訊，請參閱[將 Amazon SNS 用於使用者通知搭配行動電話號碼做為訂閱者 （傳送簡訊）](https://docs.aws.amazon.com/sns/latest/dg/sns-mobile-phone-number-as-subscriber.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)。

## 取得 SMS 屬性
<a name="get-sms-attributes"></a>

若要擷取簡訊的預設設定，請使用 [GetSMSAttributes](https://docs.aws.amazon.com/sns/latest/api/API_GetSMSAttributes.html) 操作。

本範例將取得 `DefaultSMSType` 屬性。此屬性控制著簡訊的傳送方式，`Promotional` 會將訊息交付最佳化為產生最低的成本，而 `Transactional` 會將訊息交付最佳化為達成最高的可靠性。

 **匯入** 

```
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->getSMSAttributes([
        'attributes' => ['DefaultSMSType'],
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    error_log($e->getMessage());
}
```

## 設定 SMS 屬性
<a name="set-sms-attributes"></a>

若要更新簡訊的預設設定，請使用 [SetSMSAttributes](https://docs.aws.amazon.com/sns/latest/api/API_SetSMSAttributes.html) 操作。

此範例將 `DefaultSMSType` 屬性設為 `Transactional`，藉此將訊息交付最佳化為達成最高的可靠性。

 **匯入** 

```
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->SetSMSAttributes([
        'attributes' => [
            'DefaultSMSType' => 'Transactional',
        ],
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    error_log($e->getMessage());
}
```

## 檢查電話號碼是否已選擇退出
<a name="check-if-a-phone-number-has-opted-out"></a>

若要判斷指定的電話號碼擁有者是否已選擇停止從您的帳戶接收簡訊，請使用 [CheckIfPhoneNumberIsOptedOut](https://docs.aws.amazon.com/sns/latest/api/API_CheckIfPhoneNumberIsOptedOut.html) 操作。

本範例指定 E.164 格式的電話號碼，此為國際電信通訊所採用的標準。

 **匯入** 

```
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'
]);

$phone = '+1XXX5550100';

try {
    $result = $SnSclient->checkIfPhoneNumberIsOptedOut([
        'phoneNumber' => $phone,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    error_log($e->getMessage());
}
```

## 列出選擇退出的電話號碼
<a name="list-opted-out-phone-numbers"></a>

若要擷取其擁有者已選擇停止從您的帳戶接收簡訊的電話號碼清單，請使用 [ListPhoneNumbersOptedOut](https://docs.aws.amazon.com/sns/latest/api/API_ListPhoneNumbersOptedOut.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->listPhoneNumbersOptedOut();
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    error_log($e->getMessage());
}
```

## 發佈至文字訊息 (SMS 訊息）
<a name="publish-to-a-text-message-sms-message"></a>

若要直接交付文字訊息 (簡訊) 至電話號碼，請使用 [Publish](https://docs.aws.amazon.com/sns/latest/api/API_Publish.html) 操作。

本範例指定 E.164 格式的電話號碼，此為國際電信通訊所採用的標準。

每則簡訊最多可包含 140 個位元組。單次簡訊發佈動作的大小上限為 1,600 個位元組。

如需有關傳送簡訊的詳細資訊，請參閱[傳送簡訊](https://docs.aws.amazon.com/sns/latest/dg/sms_publish-to-phone.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'
]);

$message = 'This message is sent from a Amazon SNS code sample.';
$phone = '+1XXX5550100';

try {
    $result = $SnSclient->publish([
        'Message' => $message,
        'PhoneNumber' => $phone,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    error_log($e->getMessage());
}
```