可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
Updated on dec 20 '18
The args passed to publish
method now have a new format. Fixed!
How to send a SMS over AWS using PHP
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', // < 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>";
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:
Somehow Tiago's answer didn't work for me. So I had a look at the publish API from AWS-SDK. Seems like there are no parameters of SMSType
& SenderID
in publish method. Check here -
https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sns-2010-03-31.html#publish
So if you want to override these parameters, the following variation of Tiago's code should work fine -
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);
回答4:
Hope this help for people who still using PHP AWS SDK v2
Same question: https://stackoverflow.com/a/51208701/551559
You would need to add new parameter in the source code.
// 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']
回答5:
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
),
));