找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 48|回复: 0

[php] PHP发送阿里云短信验证码

[复制链接] IP属地:广东省广州市
发表于 2023-7-4 23:12:32 | 显示全部楼层 |阅读模式
  1. <?php
  2. /**
  3. * 阿里云短信验证码发送类
  4. * @author Administrator
  5. *
  6. */
  7. class Sms {
  8.     // 保存错误信息
  9.     public $error;
  10.     // Access Key ID
  11.     private $accessKeyId = '';
  12.     // Access Access Key Secret
  13.     private $accessKeySecret = '';
  14.     // 签名
  15.     private $signName = '';
  16.     // 模版ID
  17.     private $templateCode = '';
  18.     public function __construct($cofig = array()) {
  19.         /*
  20.          * 通过参数传递
  21.          * $cofig = array (
  22.             'accessKeyId' => 'xxxxxxxxxxxx',
  23.             'accessKeySecret' => 'xxxxxxxxxxxxxxxx',
  24.             'signName' => 'xxxxxxxxxx',
  25.             'templateCode' => 'xxxxxxxxxxx'
  26.         );*/
  27.         // 配置参数
  28.         $this->accessKeyId = $cofig ['accessKeyId'];
  29.         $this->accessKeySecret = $cofig ['accessKeySecret'];
  30.         $this->signName = $cofig ['signName'];
  31.         $this->templateCode = $cofig ['templateCode'];
  32.     }
  33.     private function percentEncode($string) {
  34.         $string = urlencode ( $string );
  35.         $string = preg_replace ( '/\+/', '%20', $string );
  36.         $string = preg_replace ( '/\*/', '%2A', $string );
  37.         $string = preg_replace ( '/%7E/', '~', $string );
  38.         return $string;
  39.     }
  40.     /**
  41.      * 签名
  42.      *
  43.      * @param unknown $parameters
  44.      * @param unknown $accessKeySecret
  45.      * @return string
  46.      */
  47.     private function computeSignature($parameters, $accessKeySecret) {
  48.         ksort ( $parameters );
  49.         $canonicalizedQueryString = '';
  50.         foreach ( $parameters as $key => $value ) {
  51.             $canonicalizedQueryString .= '&' . $this->percentEncode ( $key ) . '=' . $this->percentEncode ( $value );
  52.         }
  53.         $stringToSign = 'GET&%2F&' . $this->percentencode ( substr ( $canonicalizedQueryString, 1 ) );
  54.         $signature = base64_encode ( hash_hmac ( 'sha1', $stringToSign, $accessKeySecret . '&', true ) );
  55.         return $signature;
  56.     }
  57.     /**
  58.      * @param unknown $mobile
  59.      * @param unknown $verify_code
  60.      *
  61.      */
  62.     public function send_verify($mobile, $verify_code) {
  63.         $params = array (   //此处作了修改
  64.             'SignName' => $this->signName,
  65.             'Format' => 'JSON',
  66.             'Version' => '2017-05-25',
  67.             'AccessKeyId' => $this->accessKeyId,
  68.             'SignatureVersion' => '1.0',
  69.             'SignatureMethod' => 'HMAC-SHA1',
  70.             'SignatureNonce' => uniqid (),
  71.             'Timestamp' => gmdate ( 'Y-m-d\TH:i:s\Z' ),
  72.             'Action' => 'SendSms',
  73.             'TemplateCode' => $this->templateCode,
  74.             'PhoneNumbers' => $mobile,
  75.             'TemplateParam' => '{"code":"' . $verify_code . '"}'//更换为自己的实际模版
  76.         );
  77.         //var_dump($params);die;
  78.         // 计算签名并把签名结果加入请求参数
  79.         $params ['Signature'] = $this->computeSignature ( $params, $this->accessKeySecret );
  80.         // 发送请求(此处作了修改)
  81.         //$url = 'https://sms.aliyuncs.com/?' . http_build_query ( $params );
  82.         $url = 'http://dysmsapi.aliyuncs.com/?' . http_build_query ( $params );

  83.         $ch = curl_init ();
  84.         curl_setopt ( $ch, CURLOPT_URL, $url );
  85.         curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
  86.         curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
  87.         curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
  88.         curl_setopt ( $ch, CURLOPT_TIMEOUT, 10 );
  89.         $result = curl_exec ( $ch );
  90.         curl_close ( $ch );
  91.         $result = json_decode ( $result, true );
  92.         //var_dump($result);die;
  93.         if (  $result ['Code'] != 'OK') {
  94.             $this->error = $this->getErrorMessage ( $result ['Code'] );
  95.             return false;
  96.         }
  97.         return true;
  98.     }
  99.     /**
  100.      * 获取详细错误信息
  101.      *
  102.      * @param unknown $status
  103.      */
  104.     public function getErrorMessage($status) {
  105.         // 阿里云的短信 乱八七糟的(其实是用的阿里大于)
  106.         // https://api.alidayu.com/doc2/apiDetail?spm=a3142.7629140.1.19.SmdYoA&apiId=25450
  107.         $message = array (
  108.             'isv.BUSINESS_LIMIT_CONTROL' => '短信发送频率超限',
  109.             'isv.DAY_LIMIT_CONTROL' => '已经达到您在控制台设置的短信日发送量限额值',
  110.             'isv.SMS_CONTENT_ILLEGAL' => '短信内容包含禁止发送内容,请修改短信文案',
  111.             'isv.SMS_SIGN_ILLEGAL' => '签名禁止使用,请在短信服务控制台中申请符合规定的签名',
  112.             'isp.RAM_PERMISSION_DENY' => 'RAM权限不足。,请为当前使用的AK对应子账号进行授权:AliyunDysmsFullAccess(管理权限)',
  113.             'isv.OUT_OF_SERVICE' => '余额不足。余额不足时,套餐包中即使有短信额度也无法发送短信',
  114.             'isv.PRODUCT_UN_SUBSCRIPT' => '未开通云通信产品的阿里云客户',
  115.             'isv.PRODUCT_UNSUBSCRIBE' => '产品未开通',
  116.             'isv.ACCOUNT_NOT_EXISTS' => '账户不存在,使用了错误的账户名称或AK',
  117.             'isv.ACCOUNT_ABNORMAL' => '账户异常',
  118.             'isv.SMS_TEMPLATE_ILLEGAL' => '短信模板不存在,或未经审核通过',
  119.             'isv.SMS_SIGNATURE_ILLEGAL' => '签名不存在,或未经审核通过',
  120.             'isv.INVALID_PARAMETERS' => '参数格式不正确',
  121.             'isv.MOBILE_NUMBER_ILLEGAL' => '机号码格式错误',
  122.             'isv.MOBILE_COUNT_OVER_LIMIT' => '参数PhoneNumbers中指定的手机号码数量超出限制',
  123.             'isv.TEMPLATE_MISSING_PARAMETERS' => '模版缺少变量',
  124.             'isv.BLACK_KEY_CONTROL_LIMIT' => '黑名单管控',
  125.             'isv.PARAM_LENGTH_LIMIT' => '参数超出长度限制',
  126.             'isv.PARAM_NOT_SUPPORT_URL' => '不支持URL',
  127.             'isv.AMOUNT_NOT_ENOUGH' => '账户余额不足',
  128.             'SignatureDoesNotMatch' => '签名(Signature)加密错误',
  129.         );
  130.         if (isset ( $message [$status] )) {
  131.             return $message [$status];
  132.         }
  133.         return $status;
  134.     }
  135. }
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|西兴社区 ( 蜀ICP备2022005627号 )|网站地图

GMT+8, 2024-9-17 04:21 , Processed in 0.603070 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表