Alibaba.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace mall\library\sms\alibaba;
  10. use AlibabaCloud\Client\AlibabaCloud;
  11. use AlibabaCloud\Client\Exception\ClientException;
  12. use AlibabaCloud\Client\Exception\ServerException;
  13. class Alibaba {
  14. public function __construct($setting=[]){
  15. if(empty($setting["accessKeyId"])){
  16. throw new \Exception("短信配置 accessKeyId 为空");
  17. }
  18. if(empty($setting["accessSecret"])){
  19. throw new \Exception("短信配置 accessSecret 为空");
  20. }
  21. AlibabaCloud::accessKeyClient($setting["accessKeyId"],$setting["accessSecret"])
  22. ->regionId('cn-hangzhou')
  23. ->asDefaultClient();
  24. }
  25. /**
  26. * 发送短信
  27. * $data = [
  28. * "PhoneNumbers" => 接收短信的手机号码
  29. * "SignName" => 短信签名名称
  30. * "TemplateCode" => 短信模板名称 SMS_153055065
  31. * "TemplateParam" => 短信模板变量 {"code":"1111"}
  32. * ]
  33. */
  34. public function send(array $data=[]){
  35. if(is_array($data["PhoneNumbers"])){
  36. $data["PhoneNumbers"] = implode(",",$data["PhoneNumbers"]);
  37. }
  38. $query = [
  39. "RegionId"=>"cn-hangzhou",
  40. "PhoneNumbers"=>$data["PhoneNumbers"],
  41. "SignName"=>$data["SignName"],
  42. "TemplateCode"=>$data["TemplateCode"],
  43. "TemplateParam"=>$data["TemplateParam"]
  44. ];
  45. try {
  46. $result = AlibabaCloud::rpc()
  47. ->product('Dysmsapi')
  48. // ->scheme('https') // https | http
  49. ->version('2017-05-25')
  50. ->action('SendSms')
  51. ->method('POST')
  52. ->host('dysmsapi.aliyuncs.com')
  53. ->options(['query' => $query])
  54. ->request();
  55. return $result->toArray();
  56. } catch (ClientException $e) {
  57. throw new \Exception($e->getMessage(),$e->getCode());
  58. } catch (ServerException $e) {
  59. throw new \Exception($e->getMessage(),$e->getCode());
  60. }
  61. }
  62. }