分享PB可用的阿里云短信发送类
 时间 :
          2021-10-15  
          编辑 :admin 
        2./**
3. * @author CMS88.COM
4. *  阿里云SMS
5. */
6.namespace coresms;
7.
8.use coreasicConfig;
9.
10.class AliyunSms {
11.    // 保存错误信息
12.    public $error;
13.    // Access Key ID
14.    private $accessKeyId = '';
15.    // Access Access Key Secret
16.    private $accessKeySecret = '';
17.    // 签名
18.    private $signName = '';
19.    // 模版ID
20.    private $templateCode = '';
21.    public function __construct() {
22.        // 配置参数
23.        $this->accessKeyId = Config::get('sms_appid');
24.        $this->accessKeySecret = Config::get('sms_secret');
25.        $this->signName = Config::get('sms_signame');
26.        $this->templateCode = Config::get('sms_checkcodetpl');
27.    }
28.    private function percentEncode($string) {
29.        $string = urlencode ( $string );
30.        $string = preg_replace ( '/+/', '%20', $string );
31.        $string = preg_replace ( '/*/', '%2A', $string );
32.        $string = preg_replace ( '/%7E/', '~', $string );
33.        return $string;
34.    }
35.    /**
36.     * 签名
37.     * @param unknown $parameters            
38.     * @param unknown $accessKeySecret            
39.     * @return string
40.     */
41.    private function computeSignature($parameters, $accessKeySecret) {
42.        ksort( $parameters );
43.        $canonicalizedQueryString = '';
44.        foreach ( $parameters as $key => $value ) {
45.            $canonicalizedQueryString .= '&' . $this->percentEncode ( $key ) . '=' . $this->percentEncode ( $value );
46.        }
47.        $stringToSign = 'GET&%2F&' . $this->percentencode ( substr ( $canonicalizedQueryString, 1 ) );
48.        $signature = base64_encode ( hash_hmac ( 'sha1', $stringToSign, $accessKeySecret . '&', true ) );
49.        return $signature;
50.    }
51.    /**
52.     * @param unknown $mobile            
53.     * @param unknown $verify_code            
54.     *
55.     */
56.    public function send_verify($mobile, $tpljson) {
57.        $params = array (
58.            //此处作了修改
59.            'SignName' => $this->signName,
60.            'Format' => 'JSON',
61.            'Version' => '2017-05-25',
62.            'AccessKeyId' => $this->accessKeyId,
63.            'SignatureVersion' => '1.0',
64.            'SignatureMethod' => 'HMAC-SHA1',
65.            'SignatureNonce' => uniqid (),
66.            'Timestamp' => gmdate ( 'Y-m-dTH:i:s' ),
67.            'Action' => 'SendSms',
68.            'TemplateCode' => $this->templateCode,
69.            'PhoneNumbers' => $mobile,
70.            //'TemplateParam' => '{"code":"' . $verify_code . '"}' 
71.            'TemplateParam' => $tpljson   //更换为自己的实际模版
72.        );
73.        //var_dump($params);die;
74.        // 计算签名并把签名结果加入请求参数
75.        $params ['Signature'] = $this->computeSignature( $params, $this->accessKeySecret );
76.        // 发送请求(此处作了修改)
77.        //$url = 'https://sms.aliyuncs.com/?' . http_build_query ( $params );
78.        $url = 'http://dysmsapi.aliyuncs.com/?' . http_build_query ( $params );
79.        $result = json_decode( get_url($url) );
80.        if( isset($result->Code) && $result->Code=='OK' ) {
81.            return ['code'=>1,'msg'=>$result->Message];
82.        }else{
83.            return ['code'=>0,'msg'=>$result->Message];
84.        }
85.    }
86.
87.}
 
 