SmsServer.class.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * 短信服务
  4. */
  5. namespace Common\Server;
  6. require_once 'Application/Common/Common/dysms_sdk/vendor/autoload.php';
  7. use Aliyun\Core\Config;
  8. use Aliyun\Core\Profile\DefaultProfile;
  9. use Aliyun\Core\DefaultAcsClient;
  10. use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest;
  11. use Aliyun\Api\Sms\Request\V20170525\SendBatchSmsRequest;
  12. use Aliyun\Api\Sms\Request\V20170525\QuerySendDetailsRequest;
  13. // 加载区域结点配置
  14. Config::load();
  15. //use Sms\Request\V20160927 as Sms;
  16. class SmsServer {
  17. static $acsClient = null;
  18. /**
  19. * 取得AcsClient
  20. *
  21. * @return DefaultAcsClient
  22. */
  23. public function getAcsClient() {
  24. //产品名称:云通信流量服务API产品,开发者无需替换
  25. $product = "Dysmsapi";
  26. //产品域名,开发者无需替换
  27. $domain = "dysmsapi.aliyuncs.com";
  28. // TODO 此处需要替换成开发者自己的AK (https://ak-console.aliyun.com/)
  29. $accessKeyId = ACCESS_KEY_ID; // AccessKeyId
  30. $accessKeySecret = ACCESS_KEY_SECRET; // AccessKeySecret
  31. // 暂时不支持多Region
  32. $region = "cn-hangzhou";
  33. // 服务结点
  34. $endPointName = "cn-hangzhou";
  35. if(static::$acsClient == null) {
  36. //初始化acsClient,暂不支持region化
  37. $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
  38. // 增加服务结点
  39. DefaultProfile::addEndpoint($endPointName, $region, $product, $domain);
  40. // 初始化AcsClient用于发起请求
  41. static::$acsClient = new DefaultAcsClient($profile);
  42. }
  43. return static::$acsClient;
  44. }
  45. /**
  46. * 发送短信
  47. * @return stdClass
  48. */
  49. public function sendSms($tel,$TemplateCode,$param_arr) {
  50. // 初始化SendSmsRequest实例用于设置发送短信的参数
  51. $request = new SendSmsRequest();
  52. //可选-启用https协议
  53. //$request->setProtocol("https");
  54. // 必填,设置短信接收号码
  55. $request->setPhoneNumbers($tel);
  56. // 必填,设置签名名称,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
  57. $request->setSignName(SIGN_NAME);
  58. // 必填,设置模板CODE,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
  59. $request->setTemplateCode($TemplateCode);
  60. // 可选,设置模板参数, 假如模板中存在变量需要替换则为必填项
  61. $request->setTemplateParam($param_arr);
  62. //$request->setTemplateParam(json_encode($param_arr, JSON_UNESCAPED_UNICODE));
  63. // 可选,设置流水号
  64. //$request->setOutId("yourOutId");
  65. // 选填,上行短信扩展码(扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段)
  66. //$request->setSmsUpExtendCode("1234567");
  67. try {
  68. // 发起访问请求
  69. $response = $this->getAcsClient()->getAcsResponse($request);
  70. cplog('发送结果:'.var_export($response,true),'INFO');
  71. cplog('[发送成功] 手机号:'.$tel,'INFO');
  72. if ($response->Message == 'OK') {
  73. $res = array(
  74. 'status' => true,
  75. 'RequestId' => $response->RequestId,
  76. 'response' => $response
  77. );
  78. return $res;
  79. } else {
  80. $res = array(
  81. 'status' => false,
  82. 'code' => $response->Code,
  83. 'message' => $response->Message,
  84. );
  85. return $res;
  86. }
  87. }
  88. catch (\ClientException $e) {
  89. cplog('[客户错误码]:'.var_export($e->getErrorCode(),true));
  90. cplog('[客户错误信息]:'.var_export($e->getErrorMessage(),true));
  91. $res = array(
  92. 'status' => false,
  93. 'code' => $e->getErrorCode(),
  94. 'message' => $e->getErrorMessage()
  95. );
  96. return $res;
  97. }
  98. catch (\ServerException $e) {
  99. cplog('[服务错误码]:'.var_export($e->getErrorCode(),true));
  100. cplog('[服务错误信息]:'.var_export($e->getErrorMessage(),true));
  101. $res = array(
  102. 'status' => false,
  103. 'code' => $e->getErrorCode(),
  104. 'message' => $e->getErrorMessage()
  105. );
  106. return $res;
  107. }
  108. }
  109. }