使用Amazon AWS服务PHP发送SMS

时间:2016-08-03 19:42:42

标签: php amazon-web-services sdk

我在浏览亚马逊AWS PHP-sdk的文档时遇到了麻烦。

基本上,我只需要向一个号码发送标准短信。我知道这是可能的,因为亚马逊允许您直接通过此屏幕通过控制台发送消息:

Amazon Console SMS

它说了一些关于使用“发布”方法的内容,但查看该文档确实没有提供任何答案。 #Publish documentation link

感谢任何帮助或指导。我目前正在寻找使用sdk的 V2 的解决方案。

提前致谢。

6 个答案:

答案 0 :(得分:27)

没有哪个文档显示它可以与PHP一起使用。阅读Java和C#sdk我编写了有效的PHP版本。

更新于12月20日< 18;

传递给publish方法的参数现在有了新的格式。固定!

如何使用PHP

通过AWS发送SMS

首先安装 aws / aws-sdk-php 。使用作曲家:

composer require aws/aws-sdk-php

使用以下命令创建一个php文件:

require './vendor/autoload.php';
error_reporting(E_ALL);
ini_set("display_errors", 1);

$params = array(
    'credentials' => array(
        'key' => 'YOUR_KEY_HERE',
        'secret' => 'YOUR_SECRET_HERE',
    ),
    'region' => 'us-east-1', // < your aws from SNS Topic region
    'version' => 'latest'
);
$sns = new \Aws\Sns\SnsClient($params);

$args = array(
    "MessageAttributes" => [
                'AWS.SNS.SMS.SenderID' => [
                    'DataType' => 'String',
                    'StringValue' => 'YOUR_SENDER_ID'
                ],
                'AWS.SNS.SMS.SMSType' => [
                    'DataType' => 'String',
                    'StringValue' => 'Transactional'
                ]
            ],
    "Message" => "Hello World! Visit www.tiagogouvea.com.br!",
    "PhoneNumber" => "FULL_PHONE_NUMBER"
);


$result = $sns->publish($args);
echo "<pre>";
var_dump($result);
echo "</pre>";

结果必须有一个包含许多数据的数组,包括MessageId。

答案 1 :(得分:4)

如果您使用的是3.0之前的AWS SDK版本,则仍然可以创建主题并订阅SMS类型。但是从3.0开始,您可以直接向一个号码发送短信。

    $client = SnsClient::factory(array(
    'region' => 'us-east-1',
    'version' => 'latest',
    'credentials' => array(
        'key'    => 'key',
        'secret' => 'secret')
    ));

    $message = 'Your verification code is 4';
     $payload = [
    'TopicArn' => 'arn:aws:sns:XXXXX',
        'Message'          => $message,
        'MessageStructure' => 'string',
        'MessageAttribute' => [
            'AWS.SNS.SMS.SenderID' => [
                'DataType'    => 'String',
                'StringValue' => 'Sender',
            ],
            'AWS.SNS.SMS.SMSType'  => [
                'DataType'    => 'String',
                'StringValue' => 'Transactional',
            ]
        ]
    ];
    $result = $client->subscribe(array(
        'TopicArn' => 'arn:aws:sns:XXXXX',
        'Protocol' => 'sms',
        'Endpoint' => 'XXXXXXXXXXX',
    ));
 $subarn = $result['SubscriptionArn'];
 $result = $client->publish($payload);
 $result = $client->unsubscribe(array(
    'SubscriptionArn' => $subarn,
 ));

答案 2 :(得分:3)

不知怎的,蒂亚戈的回答对我不起作用。所以我看了一下AWS-SDK的发布API。好像没有SMSType&amp;的参数。发布方法中的SenderID。点击这里 -

https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sns-2010-03-31.html#publish

因此,如果您想覆盖这些参数,Tiago代码的以下变体应该可以正常工作 -

require './vendor/autoload.php';
error_reporting(E_ALL);
ini_set("display_errors", 1);

$params = array(
    'credentials' => array(
        'key' => 'YOUR_KEY_HERE',
        'secret' => 'YOUR_SECRET_HERE',
    ),
    'region' => 'us-east-1', // < your aws from SNS Topic region
    'version' => 'latest'
);
$sns = new \Aws\Sns\SnsClient($params);

$args = array(
    "MessageAttributes" => [
                'AWS.SNS.SMS.SenderID' => [
                    'DataType' => 'String',
                    'StringValue' => 'YOUR_SENDER_ID'
                ],
                'AWS.SNS.SMS.SMSType' => [
                    'DataType' => 'String',
                    'StringValue' => 'Transactional'
                ]
            ],
    "Message" => "Hello World! Visit www.tiagogouvea.com.br!",
    "PhoneNumber" => "FULL_PHONE_NUMBER"
);

$result = $sns->publish($args);

答案 3 :(得分:0)

希望此帮助仍在使用PHP AWS SDK v2的人

相同的问题:https://stackoverflow.com/a/51208701/551559

您需要在源代码中添加新参数。

// update file: aws-sdk-php/src/Aws/Sdk/Resources/sns-2010-03-31.php

'Publish' => array(
    'parameters' => array(
        'PhoneNumber' => array( // new parameter
            'type' => 'string',
            'location' => 'aws.query',
        ),
    ),
),

// You just need to publish it and include the `PhoneNumber` parameter
$snsClientResult = $snsClient->publish([
    'Message' => 'YOUR_MESSAGE',
    'PhoneNumber' => 'PHONE_NUMBER',
    'MessageStructure' => 'SMS',
    'MessageAttributes' => [
        'AWS.SNS.SMS.SenderID' => [
            'DataType' => 'String',
            'StringValue' => 'SENDER_ID',
        ],
        'AWS.SNS.SMS.SMSType' => [
            'DataType' => 'String',
            'StringValue' => 'Promotional', // Transactional
        ]
    ]
]);

// Get the response
$snsClientResult['MessageId']

答案 4 :(得分:0)

这是我在 php 中的解决方案(添加到参数:'http'=> ['verify' => false])

<?php require './vendor/autoload.php';

$params = array(
    'credentials' => array(
        'key' => 'xxxx',
        'secret' => 'xxxx',
    ),
    'region' => 'us-east-1', // < your aws from SNS Topic region
    'version' => 'latest',
    'http'    => [
        'verify' => false
    ] ); $sns = new \Aws\Sns\SnsClient($params);

$args = array(
    "MessageAttributes" => [
                // You can put your senderId here. but first you have to verify the senderid by customer support of AWS then you can use your senderId.
                // If you don't have senderId then you can comment senderId 
                // 'AWS.SNS.SMS.SenderID' => [
                //     'DataType' => 'String',
                //     'StringValue' => ''
                // ],
                'AWS.SNS.SMS.SMSType' => [
                    'DataType' => 'String',
                    'StringValue' => 'Transactional'
                ]
            ],
    "Message" => "TESTING SMS ORDUZ",
    "PhoneNumber" => "+573148832216"   // Provide phone number with country code );


$result = $sns->publish($args);

var_dump($result); // You can check the response

答案 5 :(得分:-3)

要使用“发布”操作向移动终端发送消息,例如Kindle设备或手机上的应用,您必须指定EndpointArn。

$result = $client->publish(array(
    'TopicArn' => 'string',
    'TargetArn' => 'string',
    // Message is required
    'Message' => 'string',
    'Subject' => 'string',
    'MessageStructure' => 'string',
    'MessageAttributes' => array(
        // Associative array of custom 'String' key names
        'String' => array(
            // DataType is required
            'DataType' => 'string',
            'StringValue' => 'string',
            'BinaryValue' => 'string',
        ),
        // ... repeated
    ),
));