123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- namespace app\common\server\sms\engine;
-
- use AlibabaCloud\Client\AlibabaCloud;
- use AlibabaCloud\Client\Exception\ClientException;
- use AlibabaCloud\Client\Exception\ServerException;
-
-
-
- class AliSms extends Server
- {
-
- protected $config;
- protected $mobile;
- protected $template_code;
- protected $template_param;
-
- public function __construct($config = [])
- {
- if (empty($config)) {
- $this->error = '请联系管理员配置参数';
- return false;
- }
- $this->config = $config;
- }
-
-
-
- public function setMobile($mobile = '')
- {
- $this->mobile = $mobile;
- return $this;
- }
-
-
-
-
- public function setTemplateId($template_code = '')
- {
- $this->template_code = $template_code;
- return $this;
- }
-
-
-
-
- public function setTemplateParam($template_param = '')
- {
- $this->template_param = json_encode($template_param);
- return $this;
- }
-
-
-
-
- public function send()
- {
- AlibabaCloud::accessKeyClient($this->config['app_key'], $this->config['secret_key'])
- ->regionId('cn-hangzhou')
- ->asDefaultClient();
-
- try {
- $result = AlibabaCloud::rpcRequest()
- ->product('Dysmsapi')
- ->host('dysmsapi.aliyuncs.com')
- ->version('2017-05-25')
- ->action('SendSms')
- ->method('POST')
- ->options([
- 'query' => [
- 'PhoneNumbers' => $this->mobile,
- 'SignName' => $this->config['sign'],
- 'TemplateCode' => $this->template_code,
- 'TemplateParam' => $this->template_param,
- ],
- ])
- ->request();
-
- $res = $result->toArray();
- if (isset($res['Code']) && $res['Code'] == 'OK') {
- return $res;
- }
- $message = $res['Message'] ?? $res;
- throw new \Exception('短信错误:' . $message);
-
- } catch (ClientException $e) {
- $this->error = $e->getErrorMessage();
- return false;
- } catch (ServerException $e) {
- $this->error = $e->getErrorMessage();
- return false;
- } catch (\Exception $e) {
- $this->error = $e->getMessage();
- return false;
- }
- }
- }
-
|