endpoint = "dysmsapi.ap-southeast-1.aliyuncs.com"; $this->client = new Dysmsapi($config); $this->request = new SendSmsRequest(); } public static function client($config = []) { if (self::$instance === null) { if(!$config) { $config = [ // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。 "accessKeyId" => config('sms.alisms.accessKeyId'), // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。 "accessKeySecret" => config('sms.alisms.accessKeySecret'), ]; } self::$instance = new self($config); } return self::$instance; } public function getClient() { return $this->client; } public function setRequest($phoneNumber, $templateCode, $templateParam) { $this->request->phoneNumbers = $phoneNumber; // 接收短信的手机号码 $this->request->signName = config('sms.alisms.signName'); // 短信签名名称 $this->request->templateCode = $templateCode; // 短信模板ID $this->request->templateParam = json_encode($templateParam); // 短信模板变量对应的实际值 return $this; } public function send() { $client = $this->getClient(); try{ $runtime = new RuntimeOptions(); $runtime->maxIdleConns = 3; $runtime->connectTimeout = 1000; $runtime->readTimeout = 1000; $res = $client->sendSms($this->request, $runtime); var_dump($res);exit; }catch (TeaUnableRetryError $e) { // 获取报错数据 var_dump($e->getErrorInfo()); // 获取报错信息 var_dump($e->getMessage()); // 获取最后一次报错的 Exception 实例 var_dump($e->getLastException()); // 获取最后一次请求的 Request 实例 var_dump($e->getLastRequest()); } } }