Detailed explanation of Alibaba Cloud SMS service

零下一度
Release: 2023-03-12 13:58:01
Original
3276 people have browsed it

阿里云短信支持先使用后支付的原则,价格为4分半1条。

通过SDK可以与网站功能的绑定,实现响应的短信发送功能

现已统一合并升级为:消息服务。

消息服务

阿里云消息服务(Message Service,简称 MNS)是一种高效、可靠、安全、便捷、可弹性扩展的分布式消息服务。MNS能够帮助应用开发者在他们应用的分布式组件上自由的传递数据、通知消息,构建松耦合系统。

新的SDK下载地址:

Java SDK:
Python SDK:
C# SDK:
PHP SDK:

如果你像我一样是以前申请的短信服务升级过来的,那么旧的接口与API仍然能使用,新申请的只能使用新的SDK了。

本文旨在通过PHP实现阿里云短信接口,摆脱繁琐的SDK。

短信界面通过表单方式实现。

 
   模板CODE: 
   阿里云短信模板编号
     
   用户类型:
Copy after login

注意:阿里云短信模板编号为SMS_形式,通过JS控制输入提交按钮是否可用。

<?phpdefine(&#39;IN_ECS&#39;, true);require(dirname(__FILE__) . &#39;/includes/init.php&#39;);//ECShop初始化页面文件  function https_request($url)  
        {  $curl = curl_init();  
        curl_setopt($curl, CURLOPT_URL, $url);  
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);  
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);  
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  $data = curl_exec($curl);  if (curl_errno($curl)) {return &#39;ERROR &#39;.curl_error($curl);}  
        curl_close($curl);  return $data;  
        }  
function xml_to_array($xml){  $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";  if(preg_match_all($reg, $xml, $matches)){  $count = count($matches[0]);  for($i = 0; $i < $count; $i++){  $subxml= $matches[2][$i];  $key = $matches[1][$i];  if(preg_match( $reg, $subxml )){  $arr[$key] = xml_to_array( $subxml );  
                }else{  $arr[$key] = $subxml;  
                }  
            }  
        }  return @$arr;  
    }  
function random($length = 6 , $numeric = 0) {  PHP_VERSION < &#39;4.2.0&#39; && mt_srand((double)microtime() * 1000000);  if($numeric) {  $hash = sprintf(&#39;%0&#39;.$length.&#39;d&#39;, mt_rand(0, pow(10, $length) - 1));  
        } else {  $hash = &#39;&#39;;  $chars = &#39;ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz&#39;;  $max = strlen($chars) - 1;  for($i = 0; $i < $length; $i++) {  $hash .= $chars[mt_rand(0, $max)];  
            }  
        }  return $hash;  
    }  $target = "https://sms.aliyuncs.com/?";  //阿里云短信请求地址头    $mobile_code = random(6,1);    function percentEncode($str)  
    {  // 使用urlencode编码后,将"+","*","%7E"做替换即满足ECS API规定的编码规范  $res = urlencode($str);  $res = preg_replace(&#39;/\+/&#39;, &#39;%20&#39;, $res);  $res = preg_replace(&#39;/\*/&#39;, &#39;%2A&#39;, $res);  $res = preg_replace(&#39;/%7E/&#39;, &#39;~&#39;, $res);  return $res;  
    }  
function computeSignature($parameters, $accessKeySecret)  
    {  // 将参数Key按字典顺序排序  ksort($parameters);  // 生成规范化请求字符串  $canonicalizedQueryString = &#39;&#39;;  foreach($parameters as $key => $value)  
        {  $canonicalizedQueryString .= '&' . percentEncode($key)  . '=' . percentEncode($value);  
        }  // 生成用于计算签名的字符串 stringToSign  $stringToSign = 'GET&%2F&' . percentencode(substr($canonicalizedQueryString, 1));  //echo "<br>".$stringToSign."<br>";  
        // 计算签名,注意accessKeySecret后面要加上字符'&'  $signature = base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret . '&', true));  return $signature;  
    }  // 注意使用GMT时间  date_default_timezone_set("GMT");  $dateTimeFormat = 'Y-m-d\TH:i:s\Z'; // ISO8601规范  $accessKeyId = '根据实际修改';      // 这里填写您的Access Key ID  $accessKeySecret = '根据实际修改';  // 这里填写您的Access Key Secret  $ParamString="{\"code\":\"".strval($mobile_code)."\"}";  // 发送请求 if($_REQUEST['act'] == 'aliyun_mns'){// POST表单提交请求页面动作?act=aliyun_mns/* 显示模板 */if (isset($_POST['tcode']) && isset($_POST['tuser'])) {$tcode=$_POST["tcode"];// 表单提交SMS短信模板$tuser=$_POST["tuser"]; //表单提交用户组       $sql = "SELECT DISTINCT mobile_phone FROM " .$ecs->table($tuser);$phoneNO = $db->getCol($sql);$ii=0;foreach($phoneNO as $key=>$pn)
        {if(preg_match("/^1[34578]\d{9}$/",$pn)){$data = array(  // 公共参数  'SignName'=>'根据实际修改',  
                'Format' => 'XML',  
                'Version' => '2016-09-27',  
                'AccessKeyId' => $accessKeyId,  
                'SignatureVersion' => '1.0',  
                'SignatureMethod' => 'HMAC-SHA1',  
                'SignatureNonce'=> uniqid(),  
                'Timestamp' => date($dateTimeFormat),  // 接口参数  'Action' => 'SingleSendSms',  
                'TemplateCode' =>$tcode,  
                'RecNum' => $pn,  
                'ParamString' => $ParamString  
                );  $data['Signature'] = computeSignature($data, $accessKeySecret);//给每一位用户发短信                $result = xml_to_array(https_request($target.http_build_query($data))); 
            }
        }}        /* 显示Smarty模板 */assign_query_info();$smarty->display('aliyun_mns.htm');  
    }     
?>
Copy after login

创建和查看Access Key


YourSignName 和 YourSMSTemplateCode 获取在阿里云控制台消息服务。

The above is the detailed content of Detailed explanation of Alibaba Cloud SMS service. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template