alisms.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /***
  3. * User: jun_hy
  4. * Date: 2022/7/6
  5. * Time: 15:41
  6. */
  7. function sendSmsali($phone,$template_body,$from)
  8. {
  9. $params = array();
  10. $params["To"] = $phone;
  11. $params["From"] = $from;
  12. $params["Message"] = $template_body;
  13. $params['Type'] = "NOTIFY";
  14. $content = request(
  15. "dysmsapi.aliyuncs.com",
  16. array_merge($params, array(
  17. "RegionId" => "cn-hangzhou",
  18. "Action" => "SendMessageToGlobe",
  19. "Version" => "2017-05-25",
  20. ))
  21. );
  22. if ($content->Code == 'OK') {
  23. return $content;
  24. } else {
  25. return false;
  26. }
  27. }
  28. /**
  29. * 查询发送短信状态
  30. * mobile 手机号码
  31. * TemplateParam 参数数组
  32. * TemplateCode 模版CODE
  33. */
  34. function QuerySendDetails($phone,$bizId,$times)
  35. {
  36. $params = array();
  37. $params["PhoneNumber"] = $phone;
  38. $params['BizId'] = $bizId;
  39. $params['SendDate'] = $times;
  40. $params['PageSize'] = 10;
  41. $params['CurrentPage'] = 1;
  42. $content = $this->request(
  43. "dysmsapi.aliyuncs.com",
  44. array_merge($params, array(
  45. "RegionId" => "cn-hangzhou",
  46. "Action" => "QuerySendDetails",
  47. "Version" => "2017-05-25",
  48. ))
  49. );
  50. if ($content->Code == 'OK') {
  51. return $content->SmsSendDetailDTOs;
  52. } else {
  53. return false;
  54. }
  55. }
  56. function request($domain, $params, $security = false)
  57. {
  58. $accessKeyId ='LTAI5tD31ApgijqwnZthM9SH';
  59. $accessKeySecret = 'NsEwR56E7qB8hBLBxkUqaZe4AahEyd';
  60. $apiParams = array_merge(array(
  61. "SignatureMethod" => "HMAC-SHA1",
  62. "SignatureNonce" => uniqid(mt_rand(0, 0xffff), true),
  63. "SignatureVersion" => "1.0",
  64. "AccessKeyId" => $accessKeyId,
  65. "Timestamp" => gmdate("Y-m-d\TH:i:s\Z"),
  66. "Format" => "JSON",
  67. ), $params);
  68. ksort($apiParams);
  69. $sortedQueryStringTmp = "";
  70. foreach ($apiParams as $key => $value) {
  71. $sortedQueryStringTmp .= "&" . encode($key) . "=" . encode($value);
  72. }
  73. $stringToSign = "GET&%2F&" . encode(substr($sortedQueryStringTmp, 1));
  74. $sign = base64_encode(hash_hmac("sha1", $stringToSign, $accessKeySecret . "&", true));
  75. $signature = encode($sign);
  76. $url = ($security ? 'https' : 'http') . "://{$domain}/?Signature={$signature}{$sortedQueryStringTmp}";
  77. try {
  78. $content = fetchContent($url);
  79. return json_decode($content);
  80. } catch (\Exception $e) {
  81. return false;
  82. }
  83. }
  84. function encode($str)
  85. {
  86. $res = urlencode($str);
  87. $res = preg_replace("/\+/", "%20", $res);
  88. $res = preg_replace("/\*/", "%2A", $res);
  89. $res = preg_replace("/%7E/", "~", $res);
  90. return $res;
  91. }
  92. function fetchContent($url)
  93. {
  94. $ch = curl_init();
  95. curl_setopt($ch, CURLOPT_URL, $url);
  96. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  97. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  98. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  99. "x-sdk-client" => "php/2.0.0"
  100. ));
  101. if (substr($url, 0, 5) == 'https') {
  102. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  103. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  104. }
  105. $rtn = curl_exec($ch);
  106. if ($rtn === false) {
  107. trigger_error("[CURL_" . curl_errno($ch) . "]: " . curl_error($ch), E_USER_ERROR);
  108. }
  109. curl_close($ch);
  110. return $rtn;
  111. }