AliSmsApi.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /*
  3. 阿里云SMS发送类
  4. @author:leafrainy
  5. @time:2018年09月28日
  6. @notice:参考于阿里云180928之时的官方API demo
  7. @食用方式:
  8. @单一手机号
  9. $config = array(
  10. 'accessKeyId' => "xxxx",
  11. 'accessKeySecret' => "xxxxx",
  12. 'PhoneNumbers' => "13812345678",
  13. 'SignName' => "叶雨梧桐",
  14. 'TemplateCode' => "SMS_14681201106",
  15. 'TemplateParam' => array("info"=>"哈哈哈")
  16. );
  17. $go = new AliSmsApi($config);
  18. $go->send_sms();
  19. @多个手机号
  20. $config = array(
  21. 'accessKeyId' => "xxxx",
  22. 'accessKeySecret' => "xxxxx",
  23. 'PhoneNumbers' => "13812345678,13898765432",
  24. 'SignName' => "叶雨梧桐",
  25. 'TemplateCode' => "SMS_1468121106",
  26. 'TemplateParam' => array("info"=>"哈哈哈")
  27. );
  28. $go = new AliSmsApi($config);
  29. $go->send_sms();
  30. */
  31. class AliSmsApi {
  32. //必填:是否启用https,false为不启用
  33. private $security = false;
  34. //阿里授权ak
  35. private $accessKeyId = "";
  36. //阿里授权aks
  37. private $accessKeySecret = "";
  38. //短信签名
  39. private $SignName = "";
  40. //短信模板
  41. private $TemplateCode = "";
  42. //短信内容
  43. private $TemplateParam = "";
  44. //接受手机号
  45. private $PhoneNumbers = "";
  46. public function __construct($config =array()){
  47. $this->accessKeyId = $config['accessKeyId'];
  48. $this->accessKeySecret = $config['accessKeySecret'];
  49. $this->SignName = $config['SignName'];
  50. $this->TemplateCode = $config['TemplateCode'];
  51. $this->TemplateParam = json_encode($config["TemplateParam"], JSON_UNESCAPED_UNICODE);
  52. $this->PhoneNumbers = $config['PhoneNumbers'];
  53. }
  54. //发送短信
  55. public function send_sms(){
  56. $signData = $this->sign();
  57. $url = ($this->security ? 'https' : 'http')."://dysmsapi.aliyuncs.com/";
  58. $content = $this->fetchContent($url, $signData['method'], "Signature=".$signData['signature'].$signData['sortedQueryStringTmp']);
  59. $res = json_decode($content,true);
  60. return $res;
  61. //日志记录
  62. // $res1=$this->infoLog($res);
  63. }
  64. //生成签名
  65. private function sign($method='POST'){
  66. $params = array(
  67. "PhoneNumbers" => $this->PhoneNumbers,
  68. "SignName" => $this->SignName,
  69. "TemplateCode" => $this->TemplateCode,
  70. "TemplateParam"=> $this->TemplateParam,
  71. "SignatureMethod" => "HMAC-SHA1",
  72. "SignatureNonce" => uniqid(mt_rand(0,0xffff), true),
  73. "SignatureVersion" => "1.0",
  74. "AccessKeyId" => $this->accessKeyId,
  75. "Timestamp" => gmdate("Y-m-d\TH:i:s\Z"),
  76. "Format" => "JSON",
  77. "RegionId" => "cn-hangzhou",
  78. "Action" => "SendSms",
  79. "Version" => "2017-05-25",
  80. );
  81. ksort($params);
  82. $sortedQueryStringTmp = "";
  83. foreach ($params as $key => $value) {
  84. $sortedQueryStringTmp .= "&" . $this->encode($key) . "=" . $this->encode($value);
  85. }
  86. $stringToSign = "${method}&%2F&" . $this->encode(substr($sortedQueryStringTmp, 1));
  87. $sign = base64_encode(hash_hmac("sha1", $stringToSign, $this->accessKeySecret . "&",true));
  88. $signature = $this->encode($sign);
  89. return array(
  90. "method" => "POST",
  91. "signature"=>$signature,
  92. "sortedQueryStringTmp"=>$sortedQueryStringTmp,
  93. );
  94. }
  95. //编码
  96. private function encode($str){
  97. $res = urlencode($str);
  98. $res = preg_replace("/\+/", "%20", $res);
  99. $res = preg_replace("/\*/", "%2A", $res);
  100. $res = preg_replace("/%7E/", "~", $res);
  101. return $res;
  102. }
  103. //发送请求
  104. private function fetchContent($url, $method, $body){
  105. $ch = curl_init();
  106. if($method == 'POST') {
  107. curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
  108. curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
  109. } else {
  110. $url .= '?'.$body;
  111. }
  112. curl_setopt($ch, CURLOPT_URL, $url);
  113. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  114. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  115. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  116. "x-sdk-client" => "php/2.0.0"
  117. ));
  118. if(substr($url, 0,5) == 'https') {
  119. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  120. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  121. }
  122. $rtn = curl_exec($ch);
  123. if($rtn === false) {
  124. trigger_error("[CURL_" . curl_errno($ch) . "]: " . curl_error($ch), E_USER_ERROR);
  125. }
  126. curl_close($ch);
  127. return $rtn;
  128. }
  129. //返回信息,这里可以自己写日志
  130. private function infoLog($res){
  131. $status = $res['Code'];
  132. $message = array(
  133. "isp.RAM_PERMISSION_DENY" => "RAM权限DENY",
  134. "isv.OUT_OF_SERVICE" => "业务停机",
  135. "isv.PRODUCT_UN_SUBSCRIPT" => "未开通云通信产品的阿里云客户",
  136. "isv.PRODUCT_UNSUBSCRIBE" => "产品未开通",
  137. "isv.ACCOUNT_NOT_EXISTS" => "账户不存在",
  138. "isv.ACCOUNT_ABNORMAL" => "账户异常",
  139. "isv.SMS_TEMPLATE_ILLEGAL" => "短信模板不合法",
  140. "isv.SMS_SIGNATURE_ILLEGAL" => "短信签名不合法",
  141. "isv.INVALID_PARAMETERS" => "参数异常",
  142. "isp.SYSTEM_ERROR"=>"系统错误",
  143. "isv.MOBILE_NUMBER_ILLEGAL"=>"非法手机号",
  144. "isv.MOBILE_COUNT_OVER_LIMIT"=>"手机号码数量超过限制",
  145. "isv.TEMPLATE_MISSING_PARAMETERS"=>"模板缺少变量",
  146. "isv.BUSINESS_LIMIT_CONTROL"=>"业务限流",
  147. "isv.INVALID_JSON_PARAM"=>"JSON参数不合法,只接受字符串值",
  148. "isv.BLACK_KEY_CONTROL_LIMIT"=>"黑名单管控",
  149. "isv.PARAM_LENGTH_LIMIT"=>"参数超出长度限制",
  150. "isv.PARAM_NOT_SUPPORT_URL"=>"不支持URL",
  151. "isv.AMOUNT_NOT_ENOUGH"=>"账户余额不足",
  152. );
  153. if(isset($message[$status])){
  154. return $message[$status];
  155. }
  156. return $status;
  157. }
  158. }
  159. ?>