内容简介:阿里云短信服务 PHP adk接口【原创】
不知道各位有没有接过阿里云的短信接口,以前接接口的时候,只需要四个参数(accessKeyId,accessKeySecret,signName和templateCode),但是新的接口需要七个参数(endpoint,accessid, accesskey, topicName,sign_name, template_code, template_param_key )。
首先是准备阶段:1.下载PHP sdk(最新版 链接:https://help.aliyun.com/document_detail/32381.html?spm=5176.doc51929.6.686.Jnosz5) 2.是获得前六个参数(第七个参数,是短信模板的变量)
参数获得阶段:
1.Endpoint
(https://mns.console.aliyun.com/?spm=5176.6660585.774526198.1.HCfIAd#/topics/cn-hangzhou)
2.accessed 和 accesskey
(https://ak-console.aliyun.com/?spm=5176.doc51929.2.6.idA9js#/accesskey)
3.topicName
(https://mns.console.aliyun.com/?spm=5176.doc51929.2.9.CSilDL#/home/cn-hangzhou)
4.sign_name
(https://mns.console.aliyun.com/sms?spm=5176.2020520115.103.4.BmxCwA#/sms/Sign)
5.template_code 和 template_param_key
(https://mns.console.aliyun.com/sms?spm=5176.2020520115.103.4.BmxCwA#/sms/Template)
下面是 PHP 实例代码(红色单词代表上述参数)
<?php
require_once(dirname(dirname(dirname(__FILE__))).'/mns-autoloader.php');
use AliyunMNS\Client;
use AliyunMNS\Topic;
use AliyunMNS\Constants;
use AliyunMNS\Model\MailAttributes;
use AliyunMNS\Model\SmsAttributes;
use AliyunMNS\Model\BatchSmsAttributes;
use AliyunMNS\Model\MessageAttributes;
use AliyunMNS\Exception\MnsException;
use AliyunMNS\Requests\PublishMessageRequest;
class PublishBatchSMSMessageDemo
{
public function run($phone, $salt)
{
/**
* Step 1. 初始化Client
*/
6. $this->endPoint = " <span style="color: #ff0000;">Endpoint</span> "; // eg. http://1234567890123456.mns.cn-shenzhen.aliyuncs.com
$this->accessId = " <span style="color: #ff0000;">accessid</span> ";
$this->accessKey = "<span style="color: #ff0000;"> accesskey</span> ";
$this->client = new Client($this->endPoint, $this->accessId, $this->accessKey);
/**
* Step 2. 获取主题引用
*/
7. $topicName = " topicName ";
8. $topic = $this->client->getTopicRef($topicName);
/**
* Step 3. 生成SMS消息属性
*/
// 3.1 设置发送短信的签名(SMSSignName)和模板(SMSTemplateCode)
9. $batchSmsAttributes = new BatchSmsAttributes("<span style="color: #ff0000;">sign_name</span> ", " <span style="color: #ff0000;">template_code</span> ");
// 3.2 (如果在短信模板中定义了参数)指定短信模板中对应参数的值
$batchSmsAttributes->addReceiver($phone, array("<span style="color: #ff0000;">template_param_key</span> " => $salt)); 、、、、//$batchSmsAttributes->addReceiver("YourReceiverPhoneNumber2", array("YourSMSTemplateParamKey1" => "value1"));
$messageAttributes = new MessageAttributes(array($batchSmsAttributes));
/**
* Step 4. 设置SMS消息体(必须)
*
* 注:目前暂时不支持消息内容为空,需要指定消息内容,不为空即可。
*/
$messageBody = "smsmessage";
/**
* Step 5. 发布SMS消息
*/
$request = new PublishMessageRequest($messageBody, $messageAttributes);
try
{
$res = $topic->publishMessage($request);
echo $res->isSucceed();
echo "\n";
echo $res->getMessageId();
echo "\n";
}
catch (MnsException $e)
{
echo $e;
echo "\n";
}
}
}
$instance = new PublishBatchSMSMessageDemo();
$instance->run();
?>
一定要注意两点,一是传入的手机号($phone)和短信模板的变量($salt)为字符串类型,否则短信发送失败
二是各个参数一定要获得正确,否则也是失败
转载时请注明出处及相应链接,本文永久地址:http://blog.it985.com/21455.html
微信打赏
支付宝打赏
感谢您对作者ada的打赏,我们会更加努力! 如果您想成为作者,请点我
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Music Recommendation and Discovery
Òscar Celma / Springer / 2010-9-7 / USD 49.95
With so much more music available these days, traditional ways of finding music have diminished. Today radio shows are often programmed by large corporations that create playlists drawn from a limited......一起来看看 《Music Recommendation and Discovery》 这本书的介绍吧!
图片转BASE64编码
在线图片转Base64编码工具
XML、JSON 在线转换
在线XML、JSON转换工具