From b8d50f6f1153e7d60ac246a6ad79f43bf97759b1 Mon Sep 17 00:00:00 2001 From: uspilot <23472538+uspilot@users.noreply.github.com> Date: Wed, 14 Oct 2020 23:15:34 +0800 Subject: [PATCH] Initial commit --- .gitattributes | 2 + .gitignore | 1 + AlibabaSms.php | 191 +++++++++++++++++++++++++++++++++++++++++++++++++ LICENSE | 21 ++++++ README.md | 2 + composer.json | 20 ++++++ ini/sms.php | 13 ++++ 7 files changed, 250 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 AlibabaSms.php create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 ini/sms.php diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/AlibabaSms.php b/AlibabaSms.php new file mode 100644 index 0000000..70ef367 --- /dev/null +++ b/AlibabaSms.php @@ -0,0 +1,191 @@ +Init($iniFile, $useProxy); + } + + /** + * @param string $iniFile + * @param bool $useProxy + * @return AlibabaSms + * @throws Exception + */ + public function Init(string $iniFile = 'ini/sms.php', bool $useProxy = false) + { + $this->PhoneNumbers = ''; + $this->ActiveSignName = ''; + $this->ActiveTemplate = ''; + $this->OutId = ''; + $this->result = []; + $this->SmsUpExtendCode = []; + $this->TemplateParam = []; + + if (!file_exists($iniFile)) throw new Exception('INI file not found',9001); + $iniParser = new IniParser($iniFile); + $ini = $iniParser->parse(); + if (empty($ini['SignName'])) throw new Exception('Alibaba `SignName` is missing in INI file',9002); + if (empty($ini['Template'])) throw new Exception('Alibaba `Template` is missing in INI file',9002); + if (empty($ini['AccessKeyId'])) throw new Exception('Alibaba `AccessKeyId` is missing in INI file',9002); + if (empty($ini['AccessKeySecret'])) throw new Exception('Alibaba `AccessKeySecret` is missing in INI file',9002); + if (empty($ini['RegionId'])) $ini['RegionId'] = 'cn-hangzhou'; + + $this->SignNames = $ini['SignName']; + $this->Templates = $ini['Template']; + $this->setUseProxy($useProxy); + + AlibabaCloud::accessKeyClient($ini['AccessKeyId'], $ini['AccessKeySecret']) + ->regionId($ini['RegionId']) + ->asDefaultClient(); + if ($this->useProxy) AlibabaCloud::getDefaultClient()->proxy($this->proxy); + return $this; + } + + /** + * @return array + * @throws ClientException + * @throws ServerException + */ + public function sendSms() + { + $res = AlibabaCloud::rpc() + ->product('Dysmsapi') + ->version('2017-05-25') + ->action('SendSms') + ->method('POST') + ->options([ + 'query' => [ + 'RegionId' => $this->RegionId, + 'PhoneNumbers' => $this->PhoneNumbers, + 'SignName' => $this->ActiveSignName, + 'TemplateCode' => $this->ActiveTemplate, + 'TemplateParam' => empty($this->TemplateParam)? '' : json_encode($this->TemplateParam), + 'SmsUpExtendCode' => empty($this->SmsUpExtendCode)? '' : $this->SmsUpExtendCode, + 'OutId' => empty($this->OutId)? '' : $this->OutId, + ], + ]) + ->request(); + $this->result = $res->toArray(); + return $this->result; + } + + /** + * @param bool $useProxy + * @param string $type + * @return AlibabaSms + */ + public function setUseProxy($useProxy = true, $type = 'http') + { + if ($useProxy) { + proxy::checkProxy(false); + if ($type != 'socks5') { + $this->proxy = empty(proxy::$host)? null : proxy::$user.'@'.proxy::$host; + } + else { + seld::$proxy = empty(proxy::$socks5Host)? null : 'socks5://'.proxy::$socks5User.'@'.proxy::$socks5Host; + } + } else { + $this->proxy = null; + } + $this->useProxy = $this->proxy? true : false; + return $this; + } + + /** + * @param string $ActiveSignName + * @return AlibabaSms + */ + public function setSignName(string $ActiveSignName) + { + $this->ActiveSignName = empty($this->SignNames[$ActiveSignName])? '' : $this->SignNames[$ActiveSignName]; + return $this; + } + + /** + * @param string $ActiveTemplate + * @return AlibabaSms + */ + public function setTemplate(string $ActiveTemplate) + { + $this->ActiveTemplate = empty($this->Templates[$ActiveTemplate])? '' : $this->Templates[$ActiveTemplate]; + return $this; + } + + /** + * @param array $TemplateParam + * @return AlibabaSms + */ + public function setTemplateParam(array $TemplateParam) + { + $this->TemplateParam = $TemplateParam; + return $this; + } + + /** + * @param mixed $OutId + * @return AlibabaSms + */ + public function setOutId($OutId) + { + $this->OutId = $OutId; + return $this; + } + + /** + * @param mixed $SmsUpExtendCode + * @return AlibabaSms + */ + public function setSmsUpExtendCode($SmsUpExtendCode) + { + $this->SmsUpExtendCode = $SmsUpExtendCode; + return $this; + } + + /** + * @param mixed $PhoneNumbers + * @return AlibabaSms + */ + public function setPhoneNumbers($PhoneNumbers) + { + $this->PhoneNumbers = $PhoneNumbers; + return $this; + } + +} \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d3bc357 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 uspilot + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..a0b6a19 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# alibabasms + Send SMS message via Alibaba Cloud service diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..04a0876 --- /dev/null +++ b/composer.json @@ -0,0 +1,20 @@ +{ + "name": "uspilot/alibabasms", + "description": "PHP class to send SMS through Alibaba Cloud SMS", + "minimum-stability": "dev", + "license": "MIT", + "authors": [ + { + "name": "US Pilot" + } + ], + "require": { + "uspilot/ini_parser": "dev-master", + "alibabacloud/client": "dev-master" + }, + "autoload": { + "psr-4": { + "USPilot\\": "/" + } + } +} diff --git a/ini/sms.php b/ini/sms.php new file mode 100644 index 0000000..25253a8 --- /dev/null +++ b/ini/sms.php @@ -0,0 +1,13 @@ +;