1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- <?php
- declare(strict_types=1);
- namespace App\Master\Framework\Library\Twilio;
- use App\Master\Framework\Library\Library;
- use Twilio\Rest\Client;
- class Sms extends Library
- {
- private string $account_sid;
- private string $auth_token;
- private string $twilio_number;
- /**
- * 实例化
- */
- public function __construct()
- {
- // 获取配置信息
- $this->account_sid = (string)site('twilio_account_sid');
- $this->auth_token = (string)site('twilio_auth_token');
- $this->twilio_number = (string)site('twilio_number');
- }
- /**
- * 发送短信
- * @param string $mobile
- * @param string $body
- * @return bool
- * @throws \Twilio\Exceptions\ConfigurationException
- * @throws \Twilio\Exceptions\TwilioException
- */
- public function send(string $mobile, string $body): bool
- {
- $client = new Client($this->account_sid, $this->auth_token);
- try {
- $message = $client->messages->create($mobile, [
- 'from' => $this->twilio_number,
- 'body' => $body
- ]);
- }catch (\Exception $exception){
- return $this->error($exception->getMessage());
- }
- return $this->success('获取成功',[
- 'status' => $message->status,
- 'errorMessage' => $message->errorMessage,
- 'errorCode' => $message->errorCode,
- ]);
- }
- }
|