ChuanglanSmsApi.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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|bool $needstatus 是否需要状态报告
  50. * @param string $callbackUrl 状态回执地址
  51. */
  52. public function sendSMS( $mobile, $msg, $needstatus = 'true', $callbackUrl = '', $uid = '') {
  53. //创蓝接口参数
  54. $postArr = array (
  55. 'account' => $this->account,
  56. 'password' => $this->password,
  57. 'msg' => urlencode($msg),
  58. 'phone' => $mobile,
  59. 'report' => $needstatus,
  60. 'callbackUrl' => $callbackUrl,
  61. 'uid' => $uid //任意自定义参数(类型是字符串)
  62. );
  63. $result = $this->curlPost( self::API_SEND_URL, $postArr);
  64. return $result;
  65. }
  66. /**
  67. * 发送变量短信
  68. *
  69. * @param string $msg 短信内容
  70. * @param string $params 最多不能超过1000个参数组
  71. * @param string $extend 上行回复匹配用扩展码
  72. */
  73. public function sendVariableSMS( $msg, $params, $extend = '') {
  74. //创蓝接口参数
  75. $postArr = array (
  76. 'account' => $this->account,
  77. 'password' => $this->password,
  78. 'msg' => $msg,
  79. 'params' => $params,
  80. 'report' => 'true'
  81. );
  82. if ($extend !== '') {
  83. $postArr['extend'] = (string)$extend;
  84. }
  85. $result = $this->curlPost( self::API_VARIABLE_URL, $postArr);
  86. return $result;
  87. }
  88. /**
  89. * 查询额度
  90. *
  91. * 查询地址
  92. */
  93. public function queryBalance() {
  94. //查询参数
  95. $postArr = array (
  96. 'account' => $this->account,
  97. 'password' => $this->password,
  98. );
  99. $result = $this->curlPost(self::API_BALANCE_QUERY_URL, $postArr);
  100. return $result;
  101. }
  102. private static function getAccountConfig($accountKey)
  103. {
  104. $accountsConfig = self::env('SMS_ACCOUNTS');
  105. if (!empty($accountsConfig)) {
  106. $accounts = json_decode($accountsConfig, true);
  107. if (!is_array($accounts)) {
  108. util::fail('短信账号配置格式错误');
  109. }
  110. if (!empty($accounts[$accountKey]) && is_array($accounts[$accountKey])) {
  111. return self::validateAccountConfig($accounts[$accountKey]);
  112. }
  113. if ($accountKey !== 'default') {
  114. util::fail('短信账号不存在');
  115. }
  116. } elseif ($accountKey !== 'default') {
  117. util::fail('短信多账号配置不存在');
  118. }
  119. $account = self::env('CHUANGLAN_SMS_ACCOUNT', self::API_ACCOUNT);
  120. $password = self::env('CHUANGLAN_SMS_PASSWORD', self::API_PASSWORD);
  121. return self::validateAccountConfig([
  122. 'account' => $account,
  123. 'password' => $password,
  124. ]);
  125. }
  126. private static function validateAccountConfig($account)
  127. {
  128. if (empty($account['account']) || empty($account['password'])) {
  129. util::fail('短信账号配置不完整');
  130. }
  131. return $account;
  132. }
  133. private static function env($key, $default = null)
  134. {
  135. $value = getenv($key);
  136. if ($value !== false) {
  137. return $value;
  138. }
  139. return $default;
  140. }
  141. /**
  142. * 通过CURL发送HTTP请求
  143. * @param string $url //请求URL
  144. * @param array $postFields //请求参数
  145. * @return mixed
  146. *
  147. */
  148. private function curlPost($url,$postFields){
  149. $postFields = json_encode($postFields);
  150. $ch = curl_init ();
  151. curl_setopt( $ch, CURLOPT_URL, $url );
  152. curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
  153. 'Content-Type: application/json; charset=utf-8' //json版本需要填写 Content-Type: application/json;
  154. )
  155. );
  156. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //若果报错 name lookup timed out 报错时添加这一行代码
  157. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
  158. curl_setopt( $ch, CURLOPT_POST, 1 );
  159. curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields);
  160. curl_setopt( $ch, CURLOPT_TIMEOUT,60);
  161. curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
  162. curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
  163. $ret = curl_exec ( $ch );
  164. if (false == $ret) {
  165. $result = curl_error( $ch);
  166. } else {
  167. $rsp = curl_getinfo( $ch, CURLINFO_HTTP_CODE);
  168. if (200 != $rsp) {
  169. $result = "请求状态 ". $rsp . " " . curl_error($ch);
  170. } else {
  171. $result = $ret;
  172. }
  173. }
  174. curl_close ( $ch );
  175. return $result;
  176. }
  177. }
  178. ?>