ChuanglanSms.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Aix\Sms;
  3. use Aix\Sms\Contracts\Smser;
  4. use App\Models\SmsTemplate;
  5. use GuzzleHttp\Client;
  6. use Illuminate\Support\Facades\Cache;
  7. /**
  8. * 创蓝短信
  9. * Class AliSms
  10. * @package Aix\Sms
  11. * Auth Zhong
  12. * Date 2018/11/2
  13. */
  14. class ChuanglanSms implements Smser
  15. {
  16. /**
  17. * @var DefaultAcsClient
  18. */
  19. private $api_account;
  20. private $api_password;
  21. private $httpClient;
  22. private $api_send_url = 'http://smssh1.253.com/msg/variable/json';
  23. private $api_send_url_common = 'http://smssh1.253.com/msg/send/json';
  24. /**
  25. * SmsService constructor.
  26. * @param $app_key
  27. * @param $app_secret
  28. * @param $sign_key
  29. */
  30. public function __construct($app_key, $app_secret, $sign_key)
  31. {
  32. $this->api_account = $app_key;
  33. $this->api_password = $app_secret;
  34. $this->sign_key = $sign_key;
  35. $this->httpClient=new Client([
  36. 'http_errors' => false
  37. ]);
  38. }
  39. /**
  40. * 发送基本短信
  41. * @param string $mobile
  42. * @param SmsTemplate $template
  43. * @param array $params
  44. * @return bool
  45. * @throws \Exception
  46. */
  47. public function sendSms(string $mobile, SmsTemplate $template, array $params = [],$isVariable = true, $content = ''): bool
  48. {
  49. if(!$isVariable){
  50. $params_str = $mobile;
  51. $url = $this->api_send_url_common;
  52. //创蓝接口参数
  53. $postArr = array (
  54. 'account' => $this->api_account,
  55. 'password' => $this->api_password,
  56. 'msg' => $content,
  57. 'phone' => $params_str,
  58. 'report' => 'true'
  59. );
  60. //error_log("无变量发送参数:".json_encode($postArr)."\r\n",3,'/data/wwwroot/jucai/sms');
  61. }else{
  62. $params_str = $mobile . ',' . implode(',',$params);
  63. $url = $this->api_send_url;
  64. //创蓝接口参数
  65. $postArr = array (
  66. 'account' => $this->api_account,
  67. 'password' => $this->api_password,
  68. 'msg' => $template->value,
  69. 'params' => $params_str,
  70. 'report' => 'true'
  71. );
  72. //error_log("有变量发送参数:".json_encode($postArr)."\r\n",3,'/data/wwwroot/jucai/sms');
  73. }
  74. $postFields = json_encode($postArr);
  75. $ch = curl_init ();
  76. curl_setopt( $ch, CURLOPT_URL, $url );
  77. curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
  78. 'Content-Type: application/json; charset=utf-8' //json版本需要填写 Content-Type: application/json;
  79. )
  80. );
  81. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //若果报错 name lookup timed out 报错时添加这一行代码
  82. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
  83. curl_setopt( $ch, CURLOPT_POST, 1 );
  84. curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields);
  85. curl_setopt( $ch, CURLOPT_TIMEOUT,60);
  86. curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
  87. curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
  88. $ret = curl_exec ( $ch );
  89. curl_close ( $ch );
  90. //error_log("返回结果:".json_encode($ret)."\r\n",3,'/data/wwwroot/jucai/sms');
  91. return json_encode($ret);
  92. }
  93. /**
  94. * 发送确认短信,需要缓存验证码code
  95. * @param string $mobile
  96. * @param SmsTemplate $template
  97. * @return bool
  98. * @throws \Exception
  99. */
  100. public function sendAuthSms(string $mobile, SmsTemplate $template): bool
  101. {
  102. $params['code']=rand(100000, 999999);
  103. //error_log("{$mobile}-{$params['code']}",3,'/data/wwwroot/jucai/sms');
  104. $ret = $this->sendSms($mobile, $template, $params);
  105. Cache::put($template->alias.'_'.$mobile, $params['code'], 15);
  106. return $ret;
  107. }
  108. /**
  109. * 批量发送短信
  110. * @param array $mobiles
  111. * @param SmsTemplate $template
  112. * @param array $params
  113. * @return bool
  114. */
  115. public function sendBatchSms(array $mobiles, SmsTemplate $template, array $params = []): bool
  116. {
  117. return true;
  118. }
  119. }