Sending SMS with Amazon AWS services PHP

匿名 (未验证) 提交于 2019-12-03 01:25:01

问题:

I'm having trouble digging through the documentation for Amazon's AWS PHP-sdk.

Basically, I just need to send a standard text message to a number. I know it is possible because amazon allows you to send messages through the console directly via this screen:

It says something about using the "publish" method, but looking through that documentation really didn't provide any answers. #Publish documentation link

Any help or guidance is appreciated. I am currently looking for a solution that uses V2 of the sdk.

Thanks in advance.

回答1:

No where have a doc showing it to use with PHP. Reading the Java and C# sdk I wrote the PHP version that works.

First install aws/aws-sdk-php. Using composer:

composer require aws/aws-sdk-php

Create a php file with:

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', //  'latest' ); $sns = new \Aws\Sns\SnsClient($params);  $args = array(     "SenderID" => "SenderName",     "SMSType" => "Transactional",     "Message" => "Hello World! Visit www.tiagogouvea.com.br!",     "PhoneNumber" => "FULL_PHONE_NUMBER" );  $result = $sns->publish($args); echo "
"; var_dump($result); echo "
";

The result must have one array with many data, including MessageId.



回答2:

If you are using AWS SDK version prior to 3.0, you still have create a topic and subscribe with an SMS type. But from 3.0 onward, you could send SMS direct to a number.

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


回答3:

To use the Publish action for sending a message to a mobile endpoint, such as an app on a Kindle device or mobile phone, you must specify the 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     ), )); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!