阿里云短信验证码

config.php:

$config['sms'] = [

    'accessKeyId'=>'LTAI4GCBpRwfrPALfeDNE9QT',

    'accessKeySecret'=>'JRuHnn4RJfL6JYDuEQLHEA7LxeLZw2',

    'signName'=>'依客电商',

    'templateCode'=>'SMS_192195810'

];



Sms_model.php:

defined('BASEPATH') OR exit('No direct script access allowed');

class Sms_model extends CI_Model

{

    public $conf;

    public $accessKeyId;

    public $accessKeySecret;

    public $signName;

    public $templateCode;

    public function __construct()

    {

        parent::__construct();

        $this->conf = $this->config->item('sms');

        $this->accessKeyId = $this->conf['accessKeyId'];

        $this->accessKeySecret = $this->conf['accessKeySecret'];

        $this->signName = $this->conf['signName'];

        $this->templateCode = $this->conf['templateCode'];

    }

    /**

     * 生成签名   https://help.aliyun.com/document_detail/56189.html?spm=a2c4g.11186623.6.665.1dca51cdeNWjnQ

     * @param array $parameters  请求参数

     * @param string $accessKeySecret

     * @return string

     */

    public function computeSignature($parameters, $accessKeySecret) {

        ksort ( $parameters );

        $canonicalizedQueryString = '';

        foreach ( $parameters as $key => $value ) {

            $canonicalizedQueryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value);

        }

        $stringToSign = 'GET&%2F&' . $this->percentEncode(substr ( $canonicalizedQueryString, 1 ));

        $signature = base64_encode( hash_hmac ( 'sha1', $stringToSign, $accessKeySecret . '&', true ) );

        return $signature;

    }

    /**

     * 发送验证码

     * @param $mobile  手机号

     * @param $verify_code  验证码

     * @return bool

     */

    public function send_verify($mobile, $verify_code) {

        $params = array (   //此处作了修改

            'SignName' => $this->signName,

            'Format' => 'JSON',

            'Version' => '2017-05-25',

            'AccessKeyId' => $this->accessKeyId,

            'SignatureVersion' => '1.0',

            'SignatureMethod' => 'HMAC-SHA1',

            'SignatureNonce' => uniqid (),

            'Timestamp' => gmdate ( 'Y-m-d\TH:i:s\Z' ),

            'Action' => 'SendSms',

            'TemplateCode' => $this->templateCode,

            'PhoneNumbers' => $mobile,

            'TemplateParam' => '{"code":"' . $verify_code . '"}'

        );

        //var_dump($params);die;

        // 计算签名并把签名结果加入请求参数

        $params ['Signature'] = $this->computeSignature($params, $this->accessKeySecret);

        // 发送请求(此处作了修改)

        //$url = 'https://sms.aliyuncs.com/?' . http_build_query ( $params );

        $url = 'http://dysmsapi.aliyuncs.com/?' . http_build_query ( $params );

        $ch = curl_init();

        curl_setopt( $ch, CURLOPT_URL, $url );

        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );

        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );

        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );

        curl_setopt( $ch, CURLOPT_TIMEOUT, 10 );

        $result = curl_exec( $ch );

        curl_close( $ch );

        $result = json_decode( $result, true );

        //var_dump($result);die;

        if (isset ( $result['Code'] )) {

            return $this->getErrorMessage ( $result['Code'] );

        }

        return true;

    }

    /**

     * 获取错误信息

     * @param $status

     * @return mixed

     */

    public function getErrorMessage($status) {

        //https://help.aliyun.com/document_detail/55451.html?spm=a2c4g.11186623.6.673.4b3351cdChbIVa

       //河豚支付相关文章参考https://www.hetun99.cn

        $message = array (

            'isp.RAM_PERMISSION_DENY' => 'RAM权限DENY',

            'isv.OUT_OF_SERVICE' => '业务停机',

            'isv.PRODUCT_UN_SUBSCRIPT' => '未开通云通信产品的阿里云客户',

            'isv.PRODUCT_UNSUBSCRIBE' => '产品未开通',

            'isv.ACCOUNT_NOT_EXISTS' => '账户不存在',

            'isv.ACCOUNT_ABNORMAL' => '账户异常',

            'isv.SMS_TEMPLATE_ILLEGAL' => '短信模板不合法',

            'isv.SMS_SIGNATURE_ILLEGAL' => '短信签名不合法',

            'isv.INVALID_PARAMETERS' => '参数异常',

            'isp.SYSTEM_ERROR' => '系统错误',

            'isv.MOBILE_NUMBER_ILLEGAL' => '非法手机号',

            'isv.MOBILE_COUNT_OVER_LIMIT' => '手机号码数量超过限制',

            'isv.TEMPLATE_MISSING_PARAMETERS' => '模板缺少变量',

            'isv.BUSINESS_LIMIT_CONTROL' => '业务限流',

            'isv.INVALID_JSON_PARAM' => 'JSON参数不合法,只接受字符串值',

            'isv.BLACK_KEY_CONTROL_LIMIT' => '黑名单管控',

            'isv.PARAM_LENGTH_LIMIT' => '参数超出长度限制',

            'isv.PARAM_NOT_SUPPORT_URL' => '不支持URL',

            'isv.AMOUNT_NOT_ENOUGH' => '账户余额不足',

        );

        if (isset ( $message [$status] )) {

            return $message [$status];

        }

        return $status;

    }

    /**

     * 转换特殊字符

     * @param $string

     * @return string|string[]|null

     */

    public function percentEncode($string) {

        $string = urlencode ( $string );

        $string = preg_replace ( '/\+/', '%20', $string );

        $string = preg_replace ( '/\*/', '%2A', $string );

        $string = preg_replace ( '/%7E/', '~', $string );

        return $string;

    }

}



controller.php:

defined('BASEPATH') OR exit('No direct script access allowed');

class Sms extends CI_Controller {

    public function __construct()

    {

        parent::__construct();

        $this->load->model('Sms_model');

    }

   public function send()

   {

        //生成验证码

        $mobile = '18566725134';

        $code = mt_rand(100000, 999999 );

        //发送短信

        $ret = $this->Sms_model->send_verify($mobile, $code);

        var_dump($ret);

   }


}

你可能感兴趣的:(阿里云短信验证码)