ChuanglanSmsApi.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace common\components;
  3. header("Content-type:text/html; charset=UTF-8");
  4. /* *
  5. * 类名:ChuanglanSmsApi
  6. * 功能:创蓝接口请求类
  7. * 详细:构造创蓝短信接口请求,获取远程HTTP数据
  8. * 版本:1.3
  9. * 日期:2017-04-12
  10. * 说明:
  11. * 以下代码只是为了方便客户测试而提供的样例代码,客户可以根据自己网站的需要,按照技术文档自行编写,并非一定要使用该代码。
  12. * 该代码仅供学习和研究创蓝接口使用,只是提供一个参考。
  13. */
  14. class ChuanglanSmsApi {
  15. //参数的配置 请登录zz.253.com 获取以下API信息 ↓↓↓↓↓↓↓
  16. const API_SEND_URL='http://smsbj1.253.com/msg/send/json'; //创蓝发送短信接口URL
  17. const API_VARIABLE_URL = 'http://smsbj1.253.com/msg/variable/json';//创蓝变量短信接口URL
  18. const API_BALANCE_QUERY_URL= 'http://XXX/msg/balance/json';//创蓝短信余额查询接口URL
  19. const API_ACCOUNT= 'N2890342'; // 创蓝API账号
  20. const API_PASSWORD= 'abc178c7';// 创蓝API密码
  21. //参数的配置 请登录zz.253.com 获取以上API信息 ↑↑↑↑↑↑↑
  22. private $account = self::API_ACCOUNT;
  23. private $password = self::API_PASSWORD;
  24. public function __construct($accountKey = 'default')
  25. {
  26. $this->useAccount($accountKey);
  27. }
  28. /**
  29. * 切换短信账号
  30. *
  31. * .env 配置示例:
  32. * SMS_ACCOUNTS={"default":{"account":"Nxxxx","password":"xxxx"},"marketing":{"account":"Nyyyy","password":"yyyy"}}
  33. *
  34. * @param string $accountKey SMS_ACCOUNTS 中的账号标识
  35. * @return $this
  36. */
  37. public function useAccount($accountKey = 'default')
  38. {
  39. $account = self::getAccountConfig($accountKey);
  40. $this->account = $account['account'];
  41. $this->password = $account['password'];
  42. return $this;
  43. }
  44. /**
  45. * 发送短信
  46. *
  47. * @param string $mobile 手机号码
  48. * @param string $msg 短信内容
  49. * @param string $needstatus 是否需要状态报告
  50. */
  51. public function sendSMS( $mobile, $msg, $needstatus = 'true') {
  52. //创蓝接口参数
  53. $postArr = array (
  54. 'account' => $this->account,
  55. 'password' => $this->password,
  56. 'msg' => urlencode($msg),
  57. 'phone' => $mobile,
  58. 'report' => $needstatus,
  59. );
  60. $result = $this->curlPost( self::API_SEND_URL, $postArr);
  61. return $result;
  62. }
  63. /**
  64. * 发送变量短信
  65. *
  66. * @param string $msg 短信内容
  67. * @param string $params 最多不能超过1000个参数组
  68. * @param string $extend 上行回复匹配用扩展码
  69. */
  70. public function sendVariableSMS( $msg, $params, $extend = '') {
  71. //创蓝接口参数
  72. $postArr = array (
  73. 'account' => $this->account,
  74. 'password' => $this->password,
  75. 'msg' => $msg,
  76. 'params' => $params,
  77. 'report' => 'true'
  78. );
  79. if ($extend !== '') {
  80. $postArr['extend'] = (string)$extend;
  81. }
  82. $result = $this->curlPost( self::API_VARIABLE_URL, $postArr);
  83. return $result;
  84. }
  85. /**
  86. * 查询额度
  87. *
  88. * 查询地址
  89. */
  90. public function queryBalance() {
  91. //查询参数
  92. $postArr = array (
  93. 'account' => $this->account,
  94. 'password' => $this->password,
  95. );
  96. $result = $this->curlPost(self::API_BALANCE_QUERY_URL, $postArr);
  97. return $result;
  98. }
  99. private static function getAccountConfig($accountKey)
  100. {
  101. $accountsConfig = self::env('SMS_ACCOUNTS');
  102. if (!empty($accountsConfig)) {
  103. $accounts = json_decode($accountsConfig, true);
  104. if (!is_array($accounts)) {
  105. util::fail('短信账号配置格式错误');
  106. }
  107. if (!empty($accounts[$accountKey]) && is_array($accounts[$accountKey])) {
  108. return self::validateAccountConfig($accounts[$accountKey]);
  109. }
  110. if ($accountKey !== 'default') {
  111. util::fail('短信账号不存在');
  112. }
  113. } elseif ($accountKey !== 'default') {
  114. util::fail('短信多账号配置不存在');
  115. }
  116. $account = self::env('CHUANGLAN_SMS_ACCOUNT', self::API_ACCOUNT);
  117. $password = self::env('CHUANGLAN_SMS_PASSWORD', self::API_PASSWORD);
  118. return self::validateAccountConfig([
  119. 'account' => $account,
  120. 'password' => $password,
  121. ]);
  122. }
  123. private static function validateAccountConfig($account)
  124. {
  125. if (empty($account['account']) || empty($account['password'])) {
  126. util::fail('短信账号配置不完整');
  127. }
  128. return $account;
  129. }
  130. private static function env($key, $default = null)
  131. {
  132. $value = getenv($key);
  133. if ($value !== false) {
  134. return $value;
  135. }
  136. return $default;
  137. }
  138. /**
  139. * 通过CURL发送HTTP请求
  140. * @param string $url //请求URL
  141. * @param array $postFields //请求参数
  142. * @return mixed
  143. *
  144. */
  145. private function curlPost($url,$postFields){
  146. $postFields = json_encode($postFields);
  147. $ch = curl_init ();
  148. curl_setopt( $ch, CURLOPT_URL, $url );
  149. curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
  150. 'Content-Type: application/json; charset=utf-8' //json版本需要填写 Content-Type: application/json;
  151. )
  152. );
  153. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //若果报错 name lookup timed out 报错时添加这一行代码
  154. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
  155. curl_setopt( $ch, CURLOPT_POST, 1 );
  156. curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields);
  157. curl_setopt( $ch, CURLOPT_TIMEOUT,60);
  158. curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
  159. curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
  160. $ret = curl_exec ( $ch );
  161. if (false == $ret) {
  162. $result = curl_error( $ch);
  163. } else {
  164. $rsp = curl_getinfo( $ch, CURLINFO_HTTP_CODE);
  165. if (200 != $rsp) {
  166. $result = "请求状态 ". $rsp . " " . curl_error($ch);
  167. } else {
  168. $result = $ret;
  169. }
  170. }
  171. curl_close ( $ch );
  172. return $result;
  173. }
  174. }
  175. ?>