Alisms.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * 阿里云短信相关
  4. */
  5. namespace Alicode;
  6. use AlibabaCloud\Client\AlibabaCloud;
  7. use AlibabaCloud\Client\Exception\ClientException;
  8. use AlibabaCloud\Client\Exception\ServerException;
  9. use tests\thinkphp\library\think\config\driver\jsonTest;
  10. class Alisms
  11. {
  12. /**
  13. * 发送短信
  14. * @param $sendSmsRequest array
  15. * @param .TemplateCode array 模板code
  16. * @param .phoneNumber string 手机号码
  17. * @param .TemplateParam json 发送的内容,es:json_encode(['code' => $sendSmsRequest["code"]]);
  18. */
  19. public static function sendSmsCode($sendSmsRequest)
  20. {
  21. $config = [
  22. "AccessKeyId" => 'LTAI5tMWyaYBrBny8DFWmShy',
  23. "AccessKeySecret" => 'Y8NfAIJ08U2j9h55oOdoHZdtfRqSds',
  24. "signName" => '渔播报',
  25. "regionId" => 'cn-hangzhou',
  26. ];
  27. try {
  28. AlibabaCloud::accessKeyClient($config['AccessKeyId'], $config['AccessKeySecret'])
  29. ->regionId($config['regionId']) // replace regionId as you need
  30. ->asDefaultClient();
  31. $result = AlibabaCloud::rpc()
  32. ->product('Dysmsapi')
  33. // ->scheme('https') // https | http
  34. ->version('2017-05-25')
  35. ->action('SendSms')
  36. ->method('POST')
  37. ->options([
  38. 'query' => [
  39. 'PhoneNumbers' => $sendSmsRequest["phoneNumber"],
  40. 'SignName' => $config['signName'],
  41. 'TemplateCode' => $sendSmsRequest['TemplateCode'],
  42. 'TemplateParam' => $sendSmsRequest["TemplateParam"],
  43. 'RegionId' => $config['regionId'],
  44. ],
  45. ])
  46. ->request();
  47. return $result->toArray();
  48. } catch (ClientException $e) {
  49. echo $e->getErrorMessage() . PHP_EOL;
  50. } catch (ServerException $e) {
  51. echo $e->getErrorMessage() . PHP_EOL;
  52. }
  53. }
  54. }