简体   繁体   English

如何在 AWS SNS 上设置发件人号码(使用 PHP v3 SDK)

[英]How to set the from number on AWS SNS (Using PHP v3 SDK)

Setup设置

I've download the aws.zip file for AWS V3 SDK for PHP and extracted it into my project's folder and connected it's auto-loader into my auto-loader.我已经下载了AWS V3 SDK 的 aws.zip 文件 PHP并将其解压缩到我的项目文件夹中并将其自动加载器连接到我的自动加载器中。 I've also gone though all of the steps to create an AWS account, along with setting up SNS including acquiring a phone number to use.我还完成了创建 AWS 账户的所有步骤,以及设置 SNS,包括获取要使用的电话号码。 All of the examples work has shown in the sanity check.所有示例工作都在完整性检查中显示。

There is a little dark corner that you should know about.有一个你应该知道的黑暗角落。 You need to create a credentials file in your ~/.aws/ directory.您需要在~/.aws/目录中创建一个credentials文件。 If you are using this from a php-fpm context, that home directory might be your /var/www/ directory so you should put your credential files under /var/www/.aws/ .如果您在 php-fpm 上下文中使用它,则该主目录可能是您的/var/www/目录,因此您应该将凭证文件放在/var/www/.aws/下。

More information on the configuration file can be found here... AWS Command Line Interface - Configuration and credential file settings .可以在此处找到有关配置文件的更多信息... AWS 命令行界面 - 配置和凭证文件设置

Sanity Check完整性检查

Following the examples from the AWS SNS Documentation - Publish to a Text Message (SMS Message) .按照AWS SNS 文档中的示例 - 发布到文本消息 (SMS 消息)

Test-AWS-SNS.php测试-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());
} 

(Notes: +1AAALLL#### actually points to my cell phone number, but I'm obviously not going to put that here.) (注意: +1AAALLL####实际上指向我的手机号码,但我显然不会把它放在这里。)

The Problem问题

The problem is that there is no documentation for how to send a text message from one of the numbers that I own from AWS' SNS Long Code list.问题是没有关于如何从我在 AWS 的 SNS 长代码列表中拥有的号码之一发送短信的文档。 It always comes from the first phone number in the list and I can't find any documentation on how to change the number to another one.它总是来自列表中的第一个电话号码,我找不到任何关于如何将号码更改为另一个号码的文档。 Some help here would be great.这里的一些帮助会很棒。

Prior research先前的研究

Obviously, I've been all over the documentation and searched Stack Overflow as well.显然,我已经翻遍了文档并搜索了 Stack Overflow。 This one is pretty close, as it sets a SenderID . 这个非常接近,因为它设置了 SenderID Their documentation mentions how to do it as an optional number 9 next to the sender ID from above But I do know that it is possible, because they added the feature on Oct 23, 2020 - Amazon SNS now supports selecting the origination number when sending SMS messages -- They just haven't put any code samples.他们的文档提到如何将其作为来自上方的发件人 ID 旁边的可选数字 9但我知道这是可能的,因为他们在2020 年 10 月 23 日添加了该功能 - Amazon SNS 现在支持在发送 SMS 时选择发起号码消息——他们只是没有放置任何代码示例。

Help me Obi-Wan Kenobi, you're my only hope.帮助我欧比旺克诺比,你是我唯一的希望。

So the solutions to this, after much looking around and rubber ducking the problem while typing into the text box above, is to use a couple of different answers together.因此,解决这个问题的方法是,在四处寻找并在上面的文本框中键入时避开问题,然后一起使用几个不同的答案。 The Stack Overflow thread mentioned in prior research about setting the SenderID was very helpful to atleast getting the raw information into the AWS SDK.先前研究中提到的有关设置 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);

If you scroll into the SMS Publish To Phone documentation, you'll see the string used above AWS.SNS.SMS.SenderID and the next item down is... AWS.MM.SMS.OriginationNumber ... So maybe that will work?如果您滚动到SMS Publish To Phone文档,您会看到上面使用的字符串AWS.SNS.SMS.SenderID并且下一个项目是... AWS.MM.SMS.OriginationNumber ...所以也许这会起作用?

IT'S WORKING!它工作正常! IT'S WORKING!它工作正常!

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

Note: Obviously, +1XXXYYYZZZZ is a number from my already established long code list, and just like before +1AAALLL#### is my phone number or the phone number of the person I want to send the text message to.注意:显然, +1XXXYYYZZZZ是我已经建立的长代码列表中的一个号码,就像之前一样+1AAALLL####是我的电话号码或我要发送短信的人的电话号码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用适用于PHP的AWS开发工具包(v3)检索流对象的标头 - Retrieving headers for streaming object using AWS SDK for PHP (v3) 如何使用 PHP 和 aws-sdk v3 将大型档案上传到 Amazon Glacier? - How to upload large archives to Amazon Glacier using PHP and aws-sdk v3? AWS SDK for PHP v3中是否存在与NoSuchKeyException等效的内容? - Is there an equivalent to NoSuchKeyException in AWS SDK for PHP v3? AWS PHP SDK v3 中的响应记录 - Response logging in AWS PHP SDK v3 AWS PHP SDK v3中的createSessionsTable是否等效? - createSessionsTable equivalent in v3 of AWS PHP SDK? 如何使用 AWS SDK for PHP v3 Aws/ResultPaginator-&gt;Search 和 JMESPath 表达式按大小过滤 S3 对象? - How can I filter S3 objects by size using AWS SDK for PHP v3 Aws/ResultPaginator->Search and JMESPath expression? 如何使用PHP SDK v3从Amazon S3显示文本文件的内容 - How to display contents of text file from Amazon S3 using PHP SDK v3 PHP AWS SNS SDK - 徽章 - PHP AWS SNS SDK - Badge 使用S3进行身份验证来自服务器端时,如何从android(客户端)调用AWS S3图像URL(php + aws sdk php v3) - How to call AWS S3 image URL from android(Client) when the authentication with S3 is from server-side(php+ aws sdk php v3) AWS PHP SDK v3中的分页列表Cognito身份 - Paginating List Cognito Identities in AWS PHP SDK v3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM