博客列表 >阿里云短信

阿里云短信

毅铭
毅铭原创
2022年04月27日 12:08:55799浏览

<?php

require_once(‘引入你自己的’);
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use AlibabaCloud\Tea\Tea;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\Tea\Console\Console;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;

class SmsSingleSender {

  1. /**
  2. * 使用AK&SK初始化账号Client
  3. * @param string $accessKeyId
  4. * @param string $accessKeySecret
  5. * @return Dysmsapi Client
  6. */
  7. public static function createClient(){
  8. $config = new Config([
  9. // 您的AccessKey ID
  10. "accessKeyId" => "你自己的ID",
  11. // 您的AccessKey Secret
  12. "accessKeySecret" => "你自己的KEY"
  13. ]);
  14. // 访问的域名
  15. $config->endpoint = "dysmsapi.aliyuncs.com";
  16. return new Dysmsapi($config);
  17. }
  18. /**
  19. * @param string[] $args
  20. * @return void
  21. */
  22. public static function main($mobile,$code){
  23. $client = self::createClient();
  24. $templ = ['code'=>$code];
  25. $sendSmsRequest = new SendSmsRequest([
  26. "phoneNumbers" => $mobile,
  27. "signName" => "你自己的",
  28. "templateCode" => "你自己的",
  29. "templateParam" => json_encode($templ)
  30. ]);
  31. $resp = $client->sendSms($sendSmsRequest);
  32. if ($resp->body->code =="OK" && $resp->body->message =="OK" ){
  33. return true;
  34. }
  35. return false;
  36. }

}
$path = DIR . \DIRECTORY_SEPARATOR . ‘..’ . \DIRECTORY_SEPARATOR . ‘vendor’ . \DIRECTORY_SEPARATOR . ‘autoload.php’;
if (file_exists($path)) {
require_once $path;
}

  1. //接收参数
  2. public function index(){
  3. $mobile = request()->get('mobile/s','');
  4. if (empty($mobile)) throw new ErrorException(['msg'=>"手机号不能为空"]);
  5. $code = rand(111111,999999);
  6. $res=(new \app\api\service\SendSms())->send($mobile,$code);
  7. $res = json_decode($res,true);
  8. if ($res['SendStatusSet'][0]['Code'] =="Ok") {
  9. cache($mobile,$code,300);
  10. return json(['code'=>200,'msg'=>"发送成功"]);
  11. }
  12. return json(['code'=>201,'msg'=>"ok"]);
  13. }
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议