【问题标题】:How to set the from number on AWS SNS (Using PHP v3 SDK)如何在 AWS SNS 上设置发件人号码(使用 PHP v3 SDK)
【发布时间】:2021-11-17 18:16:20
【问题描述】:

设置

我已经下载了aws.zip file for AWS V3 SDK for PHP 并将其解压缩到我的项目文件夹中,并将它的自动加载器连接到我的自动加载器中。我还完成了创建 AWS 账户的所有步骤,以及设置 SNS,包括获取要使用的电话号码。所有示例工作都在完整性检查中显示。

有一个你应该知道的黑暗角落。您需要在~/.aws/ 目录中创建一个credentials 文件。如果您在 php-fpm 上下文中使用它,则该主目录可能是您的 /var/www/ 目录,因此您应该将您的凭据文件放在 /var/www/.aws/ 下。

更多关于配置文件的信息可以在这里找到...AWS Command Line Interface - Configuration and credential file settings

健全性检查

遵循AWS SNS Documentation - Publish to a Text Message (SMS Message) 中的示例。

Test-AWS-SNS.php

require 'vendor/autoload.php';

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

$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 = '+1AAALLL####';

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

(注意:+1AAALLL#### 实际上指向我的手机号码,但我显然不会把它放在这里。)

问题

问题在于,没有关于如何从我在 AWS 的 SNS 长代码列表中拥有的号码之一发送短信的文档。它总是来自列表中的第一个电话号码,我找不到任何关于如何将号码更改为另一个号码的文档。这里的一些帮助会很棒。

先前的研究

显然,我浏览过文档并搜索过 Stack Overflow。 This one is pretty close, as it sets a SenderID。他们的文档 mentions how to do it as an optional number 9 next to the sender ID from above 但我确实知道这是可能的,因为他们在 Oct 23, 2020 - Amazon SNS now supports selecting the origination number when sending SMS messages 上添加了该功能——他们只是没有放置任何代码示例。

帮助我欧比旺克诺比,你是我唯一的希望。

【问题讨论】:

    标签: php amazon-web-services amazon-sns


    【解决方案1】:

    因此,在四处寻找并在上面的文本框中键入时橡皮筋躲避问题之后,对此的解决方案是一起使用几个不同的答案。先前研究中提到的有关设置 SenderID 的 Stack Overflow 线程对于至少将原始信息导入 AWS SDK 非常有帮助。

    $params = array(
        'credentials' => array(
            'key' => 'iam_key',
            'secret' => 'iam_secret',
        ),
        'region' => 'ap-south-1', // < your aws from SNS Topic region
        'version' => 'latest',
        'http' => ['verify'=>false]
    );
    $sns = \Aws\Sns\SnsClient::factory($params);
    
    $msgattributes = [
            'AWS.SNS.SMS.SenderID' => [
                'DataType' => 'String',
                'StringValue' => 'Klassroom',
            ],
            'AWS.SNS.SMS.SMSType' => [
                'DataType' => 'String',
                'StringValue' => 'Transactional',
            ]
        ];
    
    $payload = array(
            'Message' => "HK test",
            "PhoneNumber" => "1234567890",
            'MessageAttributes' => $msgattributes
        );
    
    $result = $sns->publish($payload);
    

    如果您滚动到SMS Publish To Phone 文档,您会看到AWS.SNS.SMS.SenderID 上方使用的字符串,而下一项是...AWS.MM.SMS.OriginationNumber...那么也许这会起作用?

    它工作正常!它工作正常!

    require 'vendor/autoload.php';
    
    use Aws\Sns\SnsClient; 
    use Aws\Exception\AwsException;
    
    $SnSclient = new SnsClient([
        'profile' => 'default',
        'region' => 'us-east-1',
        'version' => '2010-03-31'
    ]);
    
    $msgAttributes = [
        'AWS.MM.SMS.OriginationNumber' => [
            'DataType' => 'String',
            'StringValue' => '+1XXXYYYZZZZ',
        ]
    ];
    
    $message = 'This message is sent from a Amazon SNS code sample.';
    $phone = '+1AAALLL####';
    
    try {
        $result = $SnSclient->publish([
            'Message' => $message,
            'PhoneNumber' => $phone,
            'MessageAttributes' => $msgAttributes
        ]);
        var_dump($result);
    } catch (AwsException $e) {
        // output error message if fails
        error_log($e->getMessage());
    }
    

    注意:很明显,+1XXXYYYZZZZ 是我已经建立的长代码列表中的一个号码,就像之前一样,+1AAALLL#### 是我的电话号码或我要向其发送短信的人的电话号码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-08
      相关资源
      最近更新 更多