123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- /**
- * 阿里云短信相关
- */
- namespace Alicode;
- use AlibabaCloud\Client\AlibabaCloud;
- use AlibabaCloud\Client\Exception\ClientException;
- use AlibabaCloud\Client\Exception\ServerException;
- use tests\thinkphp\library\think\config\driver\jsonTest;
- class Alisms
- {
- /**
- * 发送短信
- * @param $sendSmsRequest array
- * @param .TemplateCode array 模板code
- * @param .phoneNumber string 手机号码
- * @param .TemplateParam json 发送的内容,es:json_encode(['code' => $sendSmsRequest["code"]]);
- */
- public static function sendSmsCode($sendSmsRequest)
- {
- $config = [
- "AccessKeyId" => config('site.sms_AccessKeyId'),
- "AccessKeySecret" => config('site.AccessKeySecret'),
- "signName" => config('site.sms_signName'),
- "regionId" => config('site.sms_regionId'),
- ];
- try {
- AlibabaCloud::accessKeyClient($config['AccessKeyId'], $config['AccessKeySecret'])
- ->regionId($config['regionId']) // replace regionId as you need
- ->asDefaultClient();
- $result = AlibabaCloud::rpc()
- ->product('Dysmsapi')
- // ->scheme('https') // https | http
- ->version('2017-05-25')
- ->action('SendSms')
- ->method('POST')
- ->options([
- 'query' => [
- 'PhoneNumbers' => $sendSmsRequest["phoneNumber"],
- 'SignName' => $config['signName'],
- 'TemplateCode' => $sendSmsRequest['TemplateCode'],
- 'TemplateParam' => $sendSmsRequest["TemplateParam"],
- 'RegionId' => $config['regionId'],
- ],
- ])
- ->request();
- return $result->toArray();
- } catch (ClientException $e) {
- echo $e->getErrorMessage() . PHP_EOL;
- } catch (ServerException $e) {
- echo $e->getErrorMessage() . PHP_EOL;
- }
- }
- }
|