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; } /** * 发送短信 * * @param string $mobile 手机号码 * @param string $msg 短信内容 * @param string|bool $needstatus 是否需要状态报告 * @param string $callbackUrl 状态回执地址 */ public function sendSMS( $mobile, $msg, $needstatus = 'true', $callbackUrl = '', $uid = '') { //创蓝接口参数 $postArr = array ( 'account' => $this->account, 'password' => $this->password, 'msg' => urlencode($msg), 'phone' => $mobile, 'report' => $needstatus, 'callbackUrl' => $callbackUrl, 'uid' => $uid //任意自定义参数(类型是字符串) ); $result = $this->curlPost( self::API_SEND_URL, $postArr); return $result; } /** * 发送变量短信 * * @param string $msg 短信内容 * @param string $params 最多不能超过1000个参数组 * @param string $extend 上行回复匹配用扩展码 */ public function sendVariableSMS( $msg, $params, $extend = '') { //创蓝接口参数 $postArr = array ( 'account' => $this->account, 'password' => $this->password, 'msg' => $msg, 'params' => $params, 'report' => 'true' ); if ($extend !== '') { $postArr['extend'] = (string)$extend; } $result = $this->curlPost( self::API_VARIABLE_URL, $postArr); return $result; } /** * 查询额度 * * 查询地址 */ public function queryBalance() { //查询参数 $postArr = array ( 'account' => $this->account, 'password' => $this->password, ); $result = $this->curlPost(self::API_BALANCE_QUERY_URL, $postArr); return $result; } private static function getAccountConfig($accountKey) { $accountsConfig = self::env('SMS_ACCOUNTS'); if (!empty($accountsConfig)) { $accounts = json_decode($accountsConfig, 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('短信账号不存在'); } } elseif ($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 $default; } /** * 通过CURL发送HTTP请求 * @param string $url //请求URL * @param array $postFields //请求参数 * @return mixed * */ private function curlPost($url,$postFields){ $postFields = json_encode($postFields); $ch = curl_init (); curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json; charset=utf-8' //json版本需要填写 Content-Type: application/json; ) ); curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //若果报错 name lookup timed out 报错时添加这一行代码 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $ch, CURLOPT_POST, 1 ); curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields); curl_setopt( $ch, CURLOPT_TIMEOUT,60); curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0); $ret = curl_exec ( $ch ); if (false == $ret) { $result = curl_error( $ch); } else { $rsp = curl_getinfo( $ch, CURLINFO_HTTP_CODE); if (200 != $rsp) { $result = "请求状态 ". $rsp . " " . curl_error($ch); } else { $result = $ret; } } curl_close ( $ch ); return $result; } } ?>