Sms.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Master\Framework\Library\Twilio;
  4. use App\Master\Framework\Library\Library;
  5. use Twilio\Rest\Client;
  6. class Sms extends Library
  7. {
  8. private string $account_sid;
  9. private string $auth_token;
  10. private string $twilio_number;
  11. /**
  12. * 实例化
  13. */
  14. public function __construct()
  15. {
  16. // 获取配置信息
  17. $this->account_sid = (string)site('twilio_account_sid');
  18. $this->auth_token = (string)site('twilio_auth_token');
  19. $this->twilio_number = (string)site('twilio_number');
  20. }
  21. /**
  22. * 发送短信
  23. * @param string $mobile
  24. * @param string $body
  25. * @return bool
  26. * @throws \Twilio\Exceptions\ConfigurationException
  27. * @throws \Twilio\Exceptions\TwilioException
  28. */
  29. public function send(string $mobile, string $body): bool
  30. {
  31. $client = new Client($this->account_sid, $this->auth_token);
  32. try {
  33. $message = $client->messages->create($mobile, [
  34. 'from' => $this->twilio_number,
  35. 'body' => $body
  36. ]);
  37. }catch (\Exception $exception){
  38. return $this->error($exception->getMessage());
  39. }
  40. return $this->success('获取成功',[
  41. 'status' => $message->status,
  42. 'errorMessage' => $message->errorMessage,
  43. 'errorCode' => $message->errorCode,
  44. ]);
  45. }
  46. }