api_account = $app_key; $this->api_password = $app_secret; $this->sign_key = $sign_key; $this->httpClient=new Client([ 'http_errors' => false ]); } /** * 发送基本短信 * @param string $mobile * @param SmsTemplate $template * @param array $params * @return bool * @throws \Exception */ public function sendSms(string $mobile, SmsTemplate $template, array $params = [],$isVariable = true, $content = ''): bool { if(!$isVariable){ $params_str = $mobile; $url = $this->api_send_url_common; //创蓝接口参数 $postArr = array ( 'account' => $this->api_account, 'password' => $this->api_password, 'msg' => $content, 'phone' => $params_str, 'report' => 'true' ); //error_log("无变量发送参数:".json_encode($postArr)."\r\n",3,'/data/wwwroot/jucai/sms'); }else{ $params_str = $mobile . ',' . implode(',',$params); $url = $this->api_send_url; //创蓝接口参数 $postArr = array ( 'account' => $this->api_account, 'password' => $this->api_password, 'msg' => $template->value, 'params' => $params_str, 'report' => 'true' ); //error_log("有变量发送参数:".json_encode($postArr)."\r\n",3,'/data/wwwroot/jucai/sms'); } $postFields = json_encode($postArr); $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 ); curl_close ( $ch ); //error_log("返回结果:".json_encode($ret)."\r\n",3,'/data/wwwroot/jucai/sms'); return json_encode($ret); } /** * 发送确认短信,需要缓存验证码code * @param string $mobile * @param SmsTemplate $template * @return bool * @throws \Exception */ public function sendAuthSms(string $mobile, SmsTemplate $template): bool { $params['code']=rand(100000, 999999); //error_log("{$mobile}-{$params['code']}",3,'/data/wwwroot/jucai/sms'); $ret = $this->sendSms($mobile, $template, $params); Cache::put($template->alias.'_'.$mobile, $params['code'], 15); return $ret; } /** * 批量发送短信 * @param array $mobiles * @param SmsTemplate $template * @param array $params * @return bool */ public function sendBatchSms(array $mobiles, SmsTemplate $template, array $params = []): bool { return true; } }