AliSms.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace Mrgoon\AliSms;
  3. use Mrgoon\AliyunSmsSdk\Autoload;
  4. use Mrgoon\AliyunSmsSdk\DefaultAcsClient;
  5. use Mrgoon\AliyunSmsSdk\Profile\DefaultProfile;
  6. use Mrgoon\Dysmsapi\Request\V20170525\SendSmsRequest;
  7. class AliSms {
  8. public function sendSms($to, $template_code, $data, Array $config = null, $outId = '')
  9. {
  10. //此处需要替换成自己的AK信息
  11. if ($config) {
  12. $accessKeyId = $config['access_key'];
  13. $accessKeySecret = $config['access_secret'];
  14. $signName = $config['sign_name'];
  15. } else {
  16. $accessKeyId = config('aliyunsms.access_key');
  17. $accessKeySecret = config('aliyunsms.access_secret');
  18. $signName = config('aliyunsms.sign_name');
  19. }
  20. //短信API产品名
  21. $product = "Dysmsapi";
  22. //短信API产品域名
  23. $domain = "dysmsapi.aliyuncs.com";
  24. //暂时不支持多Region
  25. $region = "cn-hangzhou";
  26. //初始化访问的acsCleint
  27. Autoload::config();
  28. $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
  29. DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", $product, $domain);
  30. $acsClient= new DefaultAcsClient($profile);
  31. $request = new SendSmsRequest();
  32. //必填-短信接收号码
  33. $request->setPhoneNumbers($to);
  34. //必填-短信签名
  35. $request->setSignName($signName);
  36. //必填-短信模板Code
  37. $request->setTemplateCode($template_code);
  38. //选填-假如模板中存在变量需要替换则为必填(JSON格式)
  39. if ($data) {
  40. $request->setTemplateParam(json_encode($data));
  41. }
  42. //选填-发送短信流水号
  43. if ($outId) {
  44. $request->setOutId($outId);
  45. }
  46. //发起访问请求
  47. return $acsClient->getAcsResponse($request);
  48. }
  49. }
  50. //function querySendDetails() {
  51. //
  52. // //此处需要替换成自己的AK信息
  53. // $accessKeyId = "yourAccessKeyId";
  54. // $accessKeySecret = "yourAccessKeySecret";
  55. // //短信API产品名
  56. // $product = "Dysmsapi";
  57. // //短信API产品域名
  58. // $domain = "dysmsapi.aliyuncs.com";
  59. // //暂时不支持多Region
  60. // $region = "cn-hangzhou";
  61. //
  62. // //初始化访问的acsCleint
  63. // $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
  64. // DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", $product, $domain);
  65. // $acsClient= new DefaultAcsClient($profile);
  66. //
  67. // $request = new Dysmsapi\Request\V20170525\QuerySendDetailsRequest();
  68. // //必填-短信接收号码
  69. // $request->setPhoneNumber("15000000000");
  70. // //选填-短信发送流水号
  71. // $request->setBizId("abcdefgh");
  72. // //必填-短信发送日期,支持近30天记录查询,格式yyyyMMdd
  73. // $request->setSendDate("20170525");
  74. // //必填-分页大小
  75. // $request->setPageSize(10);
  76. // //必填-当前页码
  77. // $request->setContent(1);
  78. //
  79. // //发起访问请求
  80. // $acsResponse = $acsClient->getAcsResponse($request);
  81. // var_dump($acsResponse);
  82. //
  83. //}
  84. ?>