【问题标题】:Sending SMS with Amazon AWS services PHP使用 Amazon AWS 服务 PHP 发送 SMS
【发布时间】:2016-12-09 16:35:21
【问题描述】:

我在挖掘 Amazon 的 AWS PHP-sdk 的文档时遇到了麻烦。

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

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

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

提前致谢。

【问题讨论】:

  • 请问你在哪里看到那个屏幕?
  • 如果您登录 AWS 服务,点击 Amazon SNS,然后从控制台发送短信。

标签: php amazon-web-services sdk


【解决方案1】:

这是我在 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

【讨论】:

    【解决方案2】:

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

    2018 年 12 月 20 日更新

    传递给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。

    【讨论】:

    • 虽然感谢您的时间和精力,但我目前没有亚马逊帐户来测试这个。自最初发布(时间限制等)以来,我们选择了不同的 SMS 提供商。同样,我应该删除它,但它可能会在未来帮助某人。谢谢!
    • SMSType 值有错字,应为“Transactional”
    • 感谢@TiagoGouvêa 为我工作。如果有人需要更详细(复杂)的版本,AWS 论坛上的这个答案附有一个脚本,它也对我有用:forums.aws.amazon.com/thread.jspa?threadID=234658#739232
    • 我同意@SujayUN,我有一个根据这个答案构建的基本源代码,然后突然它不再工作了。我重试了相同的代码(但具有不同的值),它真的不再工作了。也许是因为它与当前现有的官方文档有所不同——sahilbathla 的回答解决了这个问题。
    • 感谢@AbelCallejo 的回复。现在修复示例。
    【解决方案3】:

    希望对仍在使用 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】:

      不知何故,蒂亚戈的回答对我不起作用。因此,我查看了 AWS-SDK 的发布 API。似乎发布方法中没有SMSTypeSenderID 的参数。在这里检查 -

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

      【讨论】:

        【解决方案5】:

        如果您使用的是 3.0 之前的 AWS 开发工具包版本,您仍然需要创建主题并使用 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,
         ));
        

        【讨论】:

          【解决方案6】:

          要使用 Publish 操作将消息发送到移动端点,例如 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
              ),
          ));
          

          【讨论】:

          • EndpointArn 是 TargetArn 的值。如果此值为空,则需要为电话号码或 TopicArn 指定一个值
          • 请更新您的示例,以反映通过电话号码向特定手机发送 SMS 消息。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-11-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-21
          • 2011-12-13
          • 2014-02-28
          相关资源
          最近更新 更多