SmsUtil.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace loyalsoft;
  3. ini_set("display_errors", "on"); # 打开错误输出
  4. require_once __DIR__ . '/msmapi/api_sdk/vendor/autoload.php';
  5. use Aliyun\Core\Config;
  6. use Aliyun\Core\Profile\DefaultProfile;
  7. use Aliyun\Core\DefaultAcsClient;
  8. use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest;
  9. use Aliyun\Api\Sms\Request\V20170525\QuerySendDetailsRequest;
  10. Config::load(); // 加载区域结点配置
  11. /**
  12. * 阿里云 - 短信Util
  13. *
  14. * @property \Aliyun\Core\DefaultAcsClient acsClient
  15. */
  16. class SmsUtil
  17. {
  18. /**
  19. * asc(http)网络通信
  20. * @var \Aliyun\Core\DefaultAcsClient 阿里
  21. */
  22. var $acsClient;
  23. /**
  24. * 单例模式
  25. * @staticvar type $smsObj
  26. * @return \SmsObject
  27. */
  28. private static function inst()
  29. {
  30. static $smsObj = null;
  31. if (is_null($smsObj)) {
  32. $smsObj = new SmsUtil(
  33. "FtwrIeI6uXLylJ0h", "SAI0ZJf7qrvngvkZCU6qgQF3d7A4dP"
  34. );
  35. }
  36. return $smsObj;
  37. }
  38. /**
  39. * 构造器
  40. *
  41. * @param string $accessKeyId 必填,AccessKeyId
  42. * @param string $accessKeySecret 必填,AccessKeySecret
  43. */
  44. private function __construct($accessKeyId, $accessKeySecret)
  45. {
  46. $product = "Dysmsapi"; // 短信API产品名
  47. $domain = "dysmsapi.aliyuncs.com"; // 短信API产品域名
  48. $region = "cn-hangzhou"; // 暂时不支持多Region
  49. $endPointName = "cn-hangzhou"; // 服务结点
  50. $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret); // 初始化用户Profile实例
  51. DefaultProfile::addEndpoint($endPointName, $region, $product, $domain); // 增加服务结点
  52. $this->acsClient = new DefaultAcsClient($profile); // 初始化AcsClient用于发起请求
  53. }
  54. /**
  55. * 发送短信范例
  56. *
  57. * @param string $signName <p>
  58. * 必填, 短信签名,应严格"签名名称"填写,参考:<a href="https://dysms.console.aliyun.com/dysms.htm#/sign">短信签名页</a>
  59. * </p>
  60. * @param string $templateCode <p>
  61. * 必填, 短信模板Code,应严格按"模板CODE"填写, 参考:<a href="https://dysms.console.aliyun.com/dysms.htm#/template">短信模板页</a>
  62. * (e.g. SMS_0001)
  63. * </p>
  64. * @param string $phoneNumbers 必填, 短信接收号码 (e.g. 12345678901)
  65. * @param array|null $templateParam <p>
  66. * 选填, 假如模板中存在变量需要替换则为必填项 (e.g. Array("code"=>"12345", "product"=>"阿里通信"))
  67. * </p>
  68. * @param string|null $outId [optional] 选填, 发送短信流水号 (e.g. 1234)
  69. * @return stdClass
  70. */
  71. static public function sendSms($signName, $templateCode, $phoneNumbers, $templateParam = null, $outId = null)
  72. {
  73. $request = new SendSmsRequest(); // 初始化SendSmsRequest实例用于设置发送短信的参数
  74. $request->setPhoneNumbers($phoneNumbers); // 必填,设置雉短信接收号码
  75. $request->setSignName($signName); // 必填,设置签名名称
  76. $request->setTemplateCode($templateCode); // 必填,设置模板CODE
  77. if ($templateParam) { // 可选,设置模板参数
  78. $request->setTemplateParam(json_encode($templateParam));
  79. }
  80. if ($outId) { // 可选,设置流水号
  81. $request->setOutId($outId);
  82. }
  83. $acsResponse = self::inst()->acsClient->getAcsResponse($request); // 发起访问请求
  84. // var_dump($acsResponse); // 打印请求结果
  85. return $acsResponse;
  86. }
  87. /**
  88. * 查询短信发送情况范例
  89. *
  90. * @param string $phoneNumbers 必填, 短信接收号码 (e.g. 12345678901)
  91. * @param string $sendDate 必填,短信发送日期,格式Ymd,支持近30天记录查询 (e.g. 20170710)
  92. * @param int $pageSize 必填,分页大小
  93. * @param int $currentPage 必填,当前页码
  94. * @param string $bizId 选填,短信发送流水号 (e.g. abc123)
  95. * @return stdClass
  96. */
  97. static public function queryDetails($phoneNumbers, $sendDate, $pageSize = 10, $currentPage = 1, $bizId = null)
  98. {
  99. $request = new QuerySendDetailsRequest(); // 初始化QuerySendDetailsRequest实例用于设置短信查询的参数
  100. $request->setPhoneNumber($phoneNumbers); // 必填,短信接收号码
  101. $request->setBizId($bizId); // 选填,短信发送流水号
  102. $request->setSendDate($sendDate); // 必填,短信发送日期,支持近30天记录查询,格式Ymd
  103. $request->setPageSize($pageSize); // 必填,分页大小
  104. $request->setCurrentPage($currentPage); // 必填,当前页码
  105. $acsResponse = self::inst()->acsClient->getAcsResponse($request); // 发起访问请求
  106. // var_dump($acsResponse); // 打印请求结果
  107. return $acsResponse;
  108. }
  109. }