ChuanglanSmsService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace App\Services;
  3. class ChuanglanSmsService
  4. {
  5. //参数的配置 请登录zz.253.com 获取以下API信息 ↓↓↓↓↓↓↓
  6. public const API_VARIABLE_URL = 'https://smssh1.253.com/msg/variable/json';//创蓝变量短信接口URL
  7. public const API_BALANCE_QUERY_URL = 'https://smssh1.253.com/msg/balance/json';//创蓝短信余额查询接口URL
  8. private $account; // 创蓝API账号
  9. private $password;// 创蓝API密码
  10. private $api_send_url; //创蓝发送短信接口URL
  11. public function __construct($config)
  12. {
  13. $this->account = $config['account'];
  14. $this->password = $config['password'];
  15. $this->api_send_url = $config['send_url'];
  16. }
  17. /**
  18. * 发送短信
  19. *
  20. * @param string $mobile 手机号码
  21. * @param string $msg 短信内容
  22. * @param string $need_status 是否需要状态报告
  23. * @return mixed
  24. */
  25. public function sendSMS($mobile, $msg, $need_status = 'true')
  26. {
  27. //创蓝接口参数
  28. $postArr = array(
  29. 'account' => $this->account,
  30. 'password' => $this->password,
  31. 'msg' => urlencode($msg),
  32. 'phone' => $mobile,
  33. 'report' => $need_status,
  34. );
  35. $result = $this->curlPost($this->api_send_url, $postArr);
  36. return $result;
  37. }
  38. /**
  39. * 发送变量短信
  40. *
  41. * @param string $msg 短信内容
  42. * @param string $params 最多不能超过1000个参数组
  43. * @return mixed
  44. */
  45. public function sendVariableSMS($msg, $params)
  46. {
  47. //创蓝接口参数
  48. $postArr = array(
  49. 'account' => $this->account,
  50. 'password' => $this->password,
  51. 'msg' => $msg,
  52. 'params' => $params,
  53. 'report' => 'true',
  54. );
  55. $result = $this->curlPost(self::API_VARIABLE_URL, $postArr);
  56. return $result;
  57. }
  58. /**
  59. * 查询额度
  60. *
  61. */
  62. public function queryBalance()
  63. {
  64. //查询参数
  65. $postArr = array(
  66. 'account' => $this->account,
  67. 'password' => $this->password,
  68. );
  69. $result = $this->curlPost(self::API_BALANCE_QUERY_URL, $postArr);
  70. return $result;
  71. }
  72. /**
  73. * 通过CURL发送HTTP请求
  74. * @param string $url //请求URL
  75. * @param array $postFields //请求参数
  76. * @return mixed
  77. */
  78. private function curlPost($url, $postFields)
  79. {
  80. $postFields = json_encode($postFields);
  81. $ch = curl_init();
  82. curl_setopt($ch, CURLOPT_URL, $url);
  83. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  84. 'Content-Type: application/json; charset=utf-8' //json版本需要填写 Content-Type: application/json;
  85. ));
  86. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  87. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  88. curl_setopt($ch, CURLOPT_POST, 1);
  89. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  90. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  91. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  92. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  93. $ret = curl_exec($ch);
  94. if (false == $ret) {
  95. $result = curl_error($ch);
  96. } else {
  97. $rsp = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  98. if (200 != $rsp) {
  99. $result = "请求状态 " . $rsp . " " . curl_error($ch);
  100. } else {
  101. $result = $ret;
  102. }
  103. }
  104. curl_close($ch);
  105. return json_decode($result, true);
  106. }
  107. }