tp3.2阿里短信

第一步
配置文件
config.php
//阿里大鱼
‘Ali_SMS’ =>array(
‘sms_temp’ =>‘短信模板’,
‘sms_sign’ =>‘签名’,
‘appkey’ =>‘appkey’,
‘secretKey’=>‘secretKey’,
),
第二步控制器写法
//获取手机验证码
public function getYzms()
{
header(“Access-Control-Allow-Origin:*”);
header(‘Access-Control-Allow-Methods:POST’);
header(‘Access-Control-Allow-Headers:x-requested-with,content-type’);
Vendor(‘alisms.Alisms’);
$mobile = hsc(I(‘param.phone’));
e n d T i m e = M ( ′ s m s ′ ) − > w h e r e ( " p h o n e = endTime = M('sms')->where("phone= endTime=M(sms)>where("phone=mobile")->order(‘end_time desc’)->limit(1)->getField(‘end_time’);
if ($endTime) {
if ((time()+120) > $endTime) {
$new = 1;
} else {
$new = 0;
}
} else {
$new = 1;
}
$brr[‘phone’] = $mobile;
// $brr[‘ip’] = request()->ip();
$oldtime = time() - 86400;
$brr[‘end_time’] = array(array(‘gt’, $oldtime), array(‘elt’, time()));
c o u n t = M ( ′ s m s ′ ) − > w h e r e ( count = M('sms')->where( count=M(sms)>where(brr)->count();
// echo M()->getLastsql();
// print_r( c o u n t ) ; / / d i e ; i f ( count); // die; if ( count);//die;if(count > 5) {
KaTeX parse error: Expected 'EOF', got '}' at position 67: …消息')); }̲ if (new == 1) {
$arr[‘code’] = rand(100000, 999999);
$arr[‘end_time’] = time() + 120;
$arr[‘phone’] = $mobile;
$arr[‘ip’] = get_client_ip();
i d = M ( ′ s m s ′ ) − > a d d ( id = M('sms')->add( id=M(sms)>add(arr);
if ($id) {
$alisms = new \Alisms(C(‘Ali_SMS.appkey’), C(‘Ali_SMS.secretKey’));
$temp_code = C(‘Ali_SMS.sms_temp’);
$paramString = ‘{“code”:"’ . $arr[‘code’] . ‘"}’;
$alisms->signName = C(‘Ali_SMS.sms_sign’);
$re = a l i s m s − > s m s e n d ( alisms->smsend( alisms>smsend(mobile, $temp_code, p a r a m S t r i n g ) ; i f ( paramString); if ( paramString);if(re[‘Code’] == ‘OK’) {
$info[‘id’] = $id;
$info[‘status’] = 1;
$info[‘info’] = ‘短信发送成功!’;
t h i s − > a j a x R e t u r n ( this->ajaxReturn( this>ajaxReturn(info);
} else {
$info[‘id’] = ‘’;
$info[‘info’] = ‘短信发送失败’;
$info[‘status’] = 0;
$this->ajaxReturn( $info);
}
} else {
$this->ajaxReturn(array(‘status’ => -1, ‘msg’ => “发送失败”));
}
} else {
$this->ajaxReturn(array(‘status’ => -1, ‘msg’ => “已发送”));
}
}

/* - 例子
TestController.php
Vendor(‘alisms.Alisms’);
$alisms = new \Alisms(C(‘Ali_SMS.appkey’),C(‘Ali_SMS.secretKey’));
$mobile = $phone;
$temp_code = C(‘Ali_SMS.sms_temp’);
KaTeX parse error: Expected '}', got 'EOF' at end of input: … = '{"code":"'.code.’"}’;
$alisms->signName = C(‘Ali_SMS.sms_sign’);
$re = a l i s m s − > s m s e n d ( alisms->smsend( alisms>smsend(mobile, t e m p c o d e , temp_code, tempcode,paramString);
if($re[‘Code’] ==‘OK’){
$info[‘status’] = 1;
i n f o [ ′ i n f o ′ ] = ′ 短 信 发 送 成 功 ! ′ ; e c h o j s o n e n c o d e ( info['info'] = '短信发送成功!'; echo json_encode( info[info]=;echojsonencode(info);
exit;
}else{
$info[‘info’] = ‘短信发送失败’;
i n f o [ ′ s t a t u s ′ ] = 0 ; e c h o j s o n e n c o d e ( info['status'] = 0; echo json_encode( info[status]=0;echojsonencode(info);
exit;
}*/
第三步加载依赖 放在vendor下
Alisms.php
<?php
/**
* 阿里云短信接口
* @author
* 示例
* KaTeX parse error: Expected 'EOF', got '\Common' at position 14: alisms = new \̲C̲o̲m̲m̲o̲n̲\Model\Alisms(accessKeyId,$accessKeySecret);
* $mobile = ‘18788830181’;
* $code = ‘SMS_36225243’;
* $paramString = ‘{“code”:“344556”}’;
* $re = a l i s m s − &gt; s m s e n d ( alisms-&gt;smsend( alisms>smsend(mobile, c o d e , code, code,paramString);
* print_r($re);
*
*/

class Alisms{
    public $config = array(
              'Format'  =>'json', //返回值的类型,支持JSON与XML。默认为XML
              'Version' =>'2017-05-25', //API版本号,为日期形式:YYYY-MM-DD,本版本对应为2016-09-27
              'SignatureMethod' =>'HMAC-SHA1', //签名方式,目前支持HMAC-SHA1
              'SignatureVersion'=>'1.0',
            );
    private    $accessKeySecret;    
    private    $http = 'http://dysmsapi.aliyuncs.com';//https://sms.aliyuncs.com/';        //短信接口
    private    $dateTimeFormat = 'Y-m-d\TH:i:s\Z'; 
    
    public     $signName = '短信签名'; //管理控制台中配置的短信签名(状态必须是验证通过)
    public     $method = 'GET';
    /**
    *发送短信
    *@AccessKeyId      阿里云申请的 Access Key ID
    *@AccessKeySecret  阿里云申请的 Access Key Secret
    */
    function __construct($accessKeyId,$accessKeySecret){
         $this->config['AccessKeyId'] = $accessKeyId;
         $this->AccessKeySecret = $accessKeySecret;
    } 
    /**
    *发送短信
    *@mobile  目标手机号,多个手机号可以逗号分隔 
    *@code 短信模板的模板CODE
    *@ParamString  短信模板中的变量;,参数格式{“no”:”123456”}, 个人用户每个变量长度必须小于15个字符
    */
    public function smsend($mobile,$code,$ParamString){
        $apiParams = $this->config;
        $apiParams["Action"]         = 'SendSms';//'SingleSendSms';
        $apiParams['TemplateCode']     = $code;  //短信模板的模板CODE
        //$apiParams['RecNum']         = $mobile;   //目标手机号,多个手机号可以逗号分隔
        //$apiParams['ParamString']     = $ParamString;   //短信模板中的变量;,此参数传递{“no”:”123456”}, 个人用户每个变量长度必须小于15个字符
        $apiParams['SignName']         = $this->signName;   //管理控制台中配置的短信签名(状态必须是验证通过)
        date_default_timezone_set("GMT");
        $apiParams["Timestamp"] = date($this->dateTimeFormat);
        $apiParams["SignatureNonce"]   = md5(md5('wbh').rand(100000,999999).uniqid()); //唯一随机数

        $apiParams['RegionId'] = 'cn-hangzhou';
        $apiParams['PhoneNumbers'] = $mobile;
        $apiParams['TemplateParam'] = $ParamString;

        $apiParams["Signature"] = $this->computeSignature($apiParams, $this->AccessKeySecret);//签名

        $tag = '?'; 
        $requestUrl = $this->http;
        foreach ($apiParams as $apiParamKey => $apiParamValue){
            $requestUrl .= $tag."$apiParamKey=" . urlencode($apiParamValue);
            $tag = '&';
        }
        return $this->postSMS($requestUrl);
    }
    private function postSMS($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output,true);
/*
        $opts = array( 
            'http'=>array( 
                'method'=>$this->method, 
                'timeout'=>600, 
                'header'=>'Content-Type: application/x-www-form-urlencoded',
            ) 
        ); 
        $html = file_get_contents($url, false, stream_context_create($opts));     
        if($html){
            return json_decode($html,true);
        }else{
            return false;
        }*/
    }

    //生成取短信签名
    private function computeSignature($parameters, $accessKeySecret){
        ksort($parameters);
        $canonicalizedQueryString = '';
        foreach($parameters as $key => $value){
            $canonicalizedQueryString .= '&' . $this->percentEncode($key). '=' . $this->percentEncode($value);
        }    
        $stringToSign = $this->method.'&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));
        $signature = $this->signString($stringToSign, $accessKeySecret."&");
        return $signature;
    }
    protected function percentEncode($str){
        $res = urlencode($str);
        $res = preg_replace('/\+/', '%20', $res);
        $res = preg_replace('/\*/', '%2A', $res);
        $res = preg_replace('/%7E/', '~', $res);
        return $res;
    }
    private function signString($source, $accessSecret){
        return    base64_encode(hash_hmac('sha1', $source, $accessSecret, true));
    }
}

!在这里插入图片描述依赖文件位置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值