Aliyun.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\common\extend\sms;
  3. class Aliyun {
  4. public $name = '阿里云短信';
  5. public $ver = '2.0';
  6. /**
  7. * 生成签名并发起请求
  8. *
  9. * @param $accessKeyId string AccessKeyId (https://ak-console.aliyun.com/)
  10. * @param $accessKeySecret string AccessKeySecret
  11. * @param $domain string API接口所在域名
  12. * @param $params array API具体参数
  13. * @param $security boolean 使用https
  14. * @param $method boolean 使用GET或POST方法请求,VPC仅支持POST
  15. * @return bool|\stdClass 返回API接口调用结果,当发生错误时返回false
  16. */
  17. public function request($accessKeyId, $accessKeySecret, $domain, $params, $security=false, $method='POST') {
  18. $apiParams = array_merge(array (
  19. "SignatureMethod" => "HMAC-SHA1",
  20. "SignatureNonce" => uniqid(mt_rand(0,0xffff), true),
  21. "SignatureVersion" => "1.0",
  22. "AccessKeyId" => $accessKeyId,
  23. "Timestamp" => gmdate("Y-m-d\TH:i:s\Z"),
  24. "Format" => "JSON",
  25. ), $params);
  26. ksort($apiParams);
  27. $sortedQueryStringTmp = "";
  28. foreach ($apiParams as $key => $value) {
  29. $sortedQueryStringTmp .= "&" . $this->encode($key) . "=" . $this->encode($value);
  30. }
  31. $stringToSign = "${method}&%2F&" . $this->encode(substr($sortedQueryStringTmp, 1));
  32. $sign = base64_encode(hash_hmac("sha1", $stringToSign, $accessKeySecret . "&",true));
  33. $signature = $this->encode($sign);
  34. $url = ($security ? 'https' : 'http')."://{$domain}/";
  35. try {
  36. $content = $this->fetchContent($url, $method, "Signature={$signature}{$sortedQueryStringTmp}");
  37. return json_decode($content,true);
  38. } catch( \Exception $e) {
  39. return false;
  40. }
  41. }
  42. private function encode($str)
  43. {
  44. $res = urlencode($str);
  45. $res = preg_replace("/\+/", "%20", $res);
  46. $res = preg_replace("/\*/", "%2A", $res);
  47. $res = preg_replace("/%7E/", "~", $res);
  48. return $res;
  49. }
  50. private function fetchContent($url, $method, $body) {
  51. $ch = curl_init();
  52. if($method == 'POST') {
  53. curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
  54. curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
  55. } else {
  56. $url .= '?'.$body;
  57. }
  58. curl_setopt($ch, CURLOPT_URL, $url);
  59. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  60. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  61. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  62. "x-sdk-client" => "php/2.0.0"
  63. ));
  64. if(substr($url, 0,5) == 'https') {
  65. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  66. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  67. }
  68. $rtn = curl_exec($ch);
  69. if($rtn === false) {
  70. // 大多由设置等原因引起,一般无法保障后续逻辑正常执行,
  71. // 所以这里触发的是E_USER_ERROR,会终止脚本执行,无法被try...catch捕获,需要用户排查环境、网络等故障
  72. trigger_error("[CURL_" . curl_errno($ch) . "]: " . curl_error($ch), E_USER_ERROR);
  73. }
  74. curl_close($ch);
  75. return $rtn;
  76. }
  77. public function submit($phone,$code,$type_flag,$type_des,$text)
  78. {
  79. if(empty($phone) || empty($code) || empty($type_flag)){
  80. return ['code'=>101,'msg'=>'参数错误'];
  81. }
  82. $appid = $GLOBALS['config']['sms']['aliyun']['appid'];
  83. $appkey = $GLOBALS['config']['sms']['aliyun']['appkey'];
  84. $sign = $GLOBALS['config']['sms']['sign'];
  85. $security = false;
  86. $tpl = $GLOBALS['config']['sms']['tpl_code_'.$type_flag];
  87. $params=[];
  88. $params['PhoneNumbers'] = $phone;
  89. $params['SignName'] = $sign;
  90. $params['TemplateCode'] = $tpl;
  91. $params['TemplateParam'] = [
  92. 'code'=>$code,
  93. ];
  94. if( is_array($params["TemplateParam"])) {
  95. $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
  96. }
  97. try {
  98. $rsp = $this->request(
  99. $appid,
  100. $appkey,
  101. "dysmsapi.aliyuncs.com",
  102. array_merge($params, array(
  103. "RegionId" => "cn-hangzhou",
  104. "Action" => "SendSms",
  105. "Version" => "2017-05-25",
  106. )),
  107. $security
  108. );
  109. if($rsp['Code'] == 'OK'){
  110. $rsp['result'] = 1;
  111. }
  112. if($rsp['result'] ==1){
  113. return ['code'=>1,'msg'=>'ok'];
  114. }
  115. return ['code'=>101,'msg'=>$rsp['Message']];
  116. }
  117. catch(\Exception $e) {
  118. return ['code'=>102,'msg'=>'发生异常请重试'];
  119. }
  120. }
  121. }