一、首先注册阿里云账号

二、开通短息服务

三、点击管理控制台

四、进行实名认证,然后点击【国内短信】,申请签名和模板

 

五、点击【帮助文档】,上面有详细讲解怎样接入短信的完整流程

六、php实现代码

首先要使用composer安装阿里云短信依赖

composer require alibabacloud/client

 

  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. use AlibabaCloud\Client\AlibabaCloud;
  5. use AlibabaCloud\Client\Exception\ClientException;
  6. use AlibabaCloud\Client\Exception\ServerException;
  7. use think\Exception;
  8. class Message extends Controller
  9. {
  10. private $config;
  11. /**
  12. * 发送短信
  13. *
  14. * @return \think\response\Json
  15. * @throws ClientException
  16. * @throws Exception
  17. * @throws \think\exception\PDOException
  18. */
  19. public function send()
  20. {
  21. if (!request()->isPost()) {
  22. return tips(\'非法请求\');
  23. }
  24. $method = input(\'_method\', \'\', \'trim\');
  25. if (strtoupper($method) != "POST") {
  26. return tips(\'非法请求\');
  27. }
  28. $mobile = input(\'mobile\', \'\', \'trim\');
  29. try {
  30. $this->config = config(\'ali_sms\');
  31. AlibabaCloud::accessKeyClient($this->config[\'accessKeyId\'], $this->config[\'accessKeySecret\'])
  32. ->regionId(\'cn-hangzhou\')
  33. ->asDefaultClient();
  34. $code = rand(100000, 999999);
  35. $data = [
  36. \'fcontent\' => $code,
  37. \'fmobile\' => $mobile,
  38. \'ftype\' => 0,
  39. \'fstatus\' => 1,
  40. \'fadd_time\' => time()
  41. ];
  42. $id = db(\'mobile_apply\')->insert($data);
  43. $result = AlibabaCloud::rpc()
  44. ->product(\'Dysmsapi\')
  45. ->version(\'2017-05-25\')
  46. ->action(\'SendSms\')
  47. ->method(\'POST\')
  48. ->options([
  49. \'query\' => [
  50. \'PhoneNumbers\' => $mobile,
  51. \'SignName\' => $this->config[\'signName\'],
  52. \'TemplateCode\' => $this->config[\'templateCodeValidate\'],
  53. \'TemplateParam\' => json_encode([\'code\' => $code]),
  54. ],
  55. ])
  56. ->request();
  57. $res = $result->toArray();
  58. db(\'mobile_apply\')->where(\'fstatus\', $id)
  59. ->update([\'fstatus\' => 2, \'fmsg\' => $result->toJson()]);
  60. if ($res[\'Code\'] != "OK") {
  61. throw new Exception($result[\'Message\']);
  62. }
  63. return tips(\'发送成功\', 1, $res);
  64. } catch (ClientException $e) {
  65. return tips($e->getErrorMessage());
  66. } catch (ServerException $e) {
  67. return tips($e->getErrorMessage());
  68. } catch (\Exception $e) {
  69. return tips($e->getMessage());
  70. }
  71. }
  72. /**
  73. * 验证短信
  74. *
  75. * @return array|string|\think\response\Json|true
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\ModelNotFoundException
  78. * @throws \think\exception\DbException
  79. */
  80. public function valid()
  81. {
  82. if (request()->isPost()) {
  83. try {
  84. $mobile = input(\'mobile\', \'\', \'trim\');
  85. $code = input(\'code\', \'\', \'trim\');
  86. $fid = input(\'fid\', \'\', \'trim\');
  87. $this->config = config(\'ali_sms\');
  88. $sms = db(\'mobile_apply\')
  89. ->where([\'fmobile\' => $mobile, \'ftype\' => [\'eq\', 0], \'fstatus\' => 2])
  90. ->order(\'fadd_time desc\')
  91. ->find();
  92. if (!$sms) {
  93. throw new Exception(\'短信验证码已过期\');
  94. }
  95. if ((time() - $sms[\'fadd_time\']) > $this->config[\'expireTime\']) {
  96. throw new Exception(\'短信验证码已经过期\');
  97. }
  98. if ($code == $sms[\'fcontent\']) {
  99. db(\'mobile_apply\')->where([\'fmobile\' => $mobile, \'ftype\' => [\'eq\', 0]])->delete();
  100. db(\'account\')->where([\'fid\'=>$fid])->update([\'faccount_mobile\'=>$mobile]);
  101. return tips(\'验证成功\', 1);
  102. } else {
  103. return tips(\'验证失败\');
  104. }
  105. } catch (\Exception $e) {
  106. return tips($e->getMessage());
  107. }
  108. } else {
  109. return tips(\'非法请求\');
  110. }
  111. }
  112. }

配置文档

  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11.  
  12. return [
  13. \'ali_sms\'=>[
  14. \'accessKeyId\'=>\'lalalalalall\',//阿里云后台申请
  15. \'accessKeySecret\'=>\'hahahahahahhah\',//阿里云后台申请
  16. \'expireTime\'=>600, // 5分钟 根据短信模板内容调整
  17. \'signName\'=>\'test商城\', // 短信签名 []中内容 阿里云后台添加
  18. \'templateCodeValidate\'=>\'SMS_169897105\' //短信模板 阿里云后台添加
  19. ],
  20. ];

 

版权声明:本文为ivy-zheng原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/ivy-zheng/p/11389547.html