ChuanglanSmsApi.php 3.7 KB

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