|
|
@@ -16,16 +16,38 @@ class ChuanglanSmsApi {
|
|
|
|
|
|
//参数的配置 请登录zz.253.com 获取以下API信息 ↓↓↓↓↓↓↓
|
|
|
const API_SEND_URL='http://smsbj1.253.com/msg/send/json'; //创蓝发送短信接口URL
|
|
|
-
|
|
|
const API_VARIABLE_URL = 'http://smsbj1.253.com/msg/variable/json';//创蓝变量短信接口URL
|
|
|
-
|
|
|
const API_BALANCE_QUERY_URL= 'http://XXX/msg/balance/json';//创蓝短信余额查询接口URL
|
|
|
|
|
|
const API_ACCOUNT= 'N2890342'; // 创蓝API账号
|
|
|
-
|
|
|
const API_PASSWORD= 'abc178c7';// 创蓝API密码
|
|
|
//参数的配置 请登录zz.253.com 获取以上API信息 ↑↑↑↑↑↑↑
|
|
|
|
|
|
+ private $account = self::API_ACCOUNT;
|
|
|
+ private $password = self::API_PASSWORD;
|
|
|
+
|
|
|
+ public function __construct($accountKey = 'default')
|
|
|
+ {
|
|
|
+ $this->useAccount($accountKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 切换短信账号
|
|
|
+ *
|
|
|
+ * .env 配置示例:
|
|
|
+ * SMS_ACCOUNTS={"default":{"account":"Nxxxx","password":"xxxx"},"marketing":{"account":"Nyyyy","password":"yyyy"}}
|
|
|
+ *
|
|
|
+ * @param string $accountKey SMS_ACCOUNTS 中的账号标识
|
|
|
+ * @return $this
|
|
|
+ */
|
|
|
+ public function useAccount($accountKey = 'default')
|
|
|
+ {
|
|
|
+ $account = self::getAccountConfig($accountKey);
|
|
|
+ $this->account = $account['account'];
|
|
|
+ $this->password = $account['password'];
|
|
|
+
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 发送短信
|
|
|
@@ -38,8 +60,8 @@ class ChuanglanSmsApi {
|
|
|
|
|
|
//创蓝接口参数
|
|
|
$postArr = array (
|
|
|
- 'account' => self::API_ACCOUNT,
|
|
|
- 'password' => self::API_PASSWORD,
|
|
|
+ 'account' => $this->account,
|
|
|
+ 'password' => $this->password,
|
|
|
'msg' => urlencode($msg),
|
|
|
'phone' => $mobile,
|
|
|
'report' => $needstatus,
|
|
|
@@ -59,8 +81,8 @@ class ChuanglanSmsApi {
|
|
|
|
|
|
//创蓝接口参数
|
|
|
$postArr = array (
|
|
|
- 'account' => self::API_ACCOUNT,
|
|
|
- 'password' => self::API_PASSWORD,
|
|
|
+ 'account' => $this->account,
|
|
|
+ 'password' => $this->password,
|
|
|
'msg' => $msg,
|
|
|
'params' => $params,
|
|
|
'report' => 'true'
|
|
|
@@ -82,13 +104,59 @@ class ChuanglanSmsApi {
|
|
|
|
|
|
//查询参数
|
|
|
$postArr = array (
|
|
|
- 'account' => self::API_ACCOUNT,
|
|
|
- 'password' => self::API_PASSWORD,
|
|
|
+ 'account' => $this->account,
|
|
|
+ 'password' => $this->password,
|
|
|
);
|
|
|
$result = $this->curlPost(self::API_BALANCE_QUERY_URL, $postArr);
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
+ private static function getAccountConfig($accountKey)
|
|
|
+ {
|
|
|
+ $accounts = self::env('SMS_ACCOUNTS');
|
|
|
+ if (!empty($accounts)) {
|
|
|
+ $accounts = json_decode($accounts, true);
|
|
|
+ if (!is_array($accounts)) {
|
|
|
+ util::fail('短信账号配置格式错误');
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!empty($accounts[$accountKey]) && is_array($accounts[$accountKey])) {
|
|
|
+ return self::validateAccountConfig($accounts[$accountKey]);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($accountKey !== 'default') {
|
|
|
+ util::fail('短信账号不存在');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $account = self::env('CHUANGLAN_SMS_ACCOUNT', self::API_ACCOUNT);
|
|
|
+ $password = self::env('CHUANGLAN_SMS_PASSWORD', self::API_PASSWORD);
|
|
|
+
|
|
|
+ return self::validateAccountConfig([
|
|
|
+ 'account' => $account,
|
|
|
+ 'password' => $password,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static function validateAccountConfig($account)
|
|
|
+ {
|
|
|
+ if (empty($account['account']) || empty($account['password'])) {
|
|
|
+ util::fail('短信账号配置不完整');
|
|
|
+ }
|
|
|
+
|
|
|
+ return $account;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static function env($key, $default = null)
|
|
|
+ {
|
|
|
+ $value = getenv($key);
|
|
|
+ if ($value !== false) {
|
|
|
+ return $value;
|
|
|
+ }
|
|
|
+
|
|
|
+ return '';
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 通过CURL发送HTTP请求
|
|
|
* @param string $url //请求URL
|