一款大佬分享的簡(jiǎn)單方便阿里云短信封裝代碼,不用官網(wǎng)復(fù)雜的SDK,快速上手。由CMS88適配成pbootcms,可以拿來(lái)快速制作短信發(fā)送功能。
封裝代碼
accessKeyId = Config::get('sms_appid'); $this->accessKeySecret = Config::get('sms_secret'); $this->signName = Config::get('sms_signame'); $this->templateCode = Config::get('sms_checkcodetpl'); } private function percentEncode($string) { $string = urlencode ( $string ); $string = preg_replace ( '/+/', '%20', $string ); $string = preg_replace ( '/*/', '%2A', $string ); $string = preg_replace ( '/%7E/', '~', $string ); return $string; } /** * 簽名 * @param unknown $parameters * @param unknown $accessKeySecret * @return string */ private 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 unknown $mobile * @param unknown $verify_code * */ public function send_verify($mobile, $tpljson) { $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-dTH:i:sZ' ), 'Action' => 'SendSms', 'TemplateCode' => $this->templateCode, 'PhoneNumbers' => $mobile, //'TemplateParam' => '{"code":"' . $verify_code . '"}' 'TemplateParam' => $tpljson //更換為自己的實(shí)際模版 ); //var_dump($params);die; // 計(jì)算簽名并把簽名結(jié)果加入請(qǐng)求參數(shù) $params ['Signature'] = $this->computeSignature( $params, $this->accessKeySecret ); // 發(fā)送請(qǐng)求(此處作了修改) //$url = 'https://sms.aliyuncs.com/?' . http_build_query ( $params ); $url = 'http://dysmsapi.aliyuncs.com/?' . http_build_query ( $params ); $result = json_decode( get_url($url) ); if( isset($result->Code) && $result->Code=='OK' ) { return ['code'=>1,'msg'=>$result->Message]; }else{ return ['code'=>0,'msg'=>$result->Message]; } } }
如果需要留言或表單手機(jī)號(hào)阿里云短信驗(yàn)證功能,可咨詢客服獲取二次開(kāi)發(fā)服務(wù)
版權(quán)聲明: 本站資源均來(lái)自互聯(lián)網(wǎng)或會(huì)員發(fā)布,如果侵犯了您的權(quán)益請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)刪除!謝謝!
轉(zhuǎn)載請(qǐng)注明: PbootCMS對(duì)接阿里云短信發(fā)送類的方法