smsUsername = env('sms.username'); $this->smsPassword = env('sms.password'); $this->sendUrl = env('sms.api_url'); } /** * * @param $code * @return string */ public function smsCodeTemplate($code):string { return '您的短信验证码为:'.$code.',用于手机号绑定,10分钟内有效。若非本人操作,请勿泄露,谨防被骗。'; } /** * * @param $mobile * @param $message * @throws FuncException */ public function sendSms($mobile,$message) { $content = $this->content_prefix . $message; $param = [ 'userName' => $this->smsUsername, 'sign' => md5($this->smsUsername . $this->smsPassword . $mobile . $content), 'mobile' => $mobile, 'content' => $content, ]; $json = json_encode($param); $result = $this->curl($json); if ($result['resultCode'] != 1) { throw new FuncException('发送失败'); } } /** * * @param $json * @return bool|string * @throws FuncException */ public function curl($json) { // 初始化cURL会话 $ch = curl_init(); // 设置cURL选项 curl_setopt($ch, CURLOPT_URL, $this->sendUrl); // 目标URL curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 返回结果而不是输出 curl_setopt($ch, CURLOPT_POST, true); // 发送POST请求 // 设置POST字段 curl_setopt($ch, CURLOPT_POSTFIELDS, $json); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); // 设置HTTP头 // 执行cURL会话 $response = curl_exec($ch); // 检查是否有错误发生 if (curl_errno($ch)) { throw new FuncException('发送失败'); } // 关闭cURL会话 curl_close($ch); return json_decode($response,true); } }