
Product
Socket Zero Introduces Automated Remediation PRs
Put vulnerability remediation on autopilot with automated security patches and intelligent dependency upgrades across your GitHub repositories.
一个基于PHP 8+的短信发送客户端库,采用驱动(driver)设计模式,提供单例模式和门面模式两种使用方式,支持配置管理、短信发送和批量发送功能,包含完整的类型声明和异常处理机制,易于扩展支持多种短信服务商。
一个基于PHP 8+的高性能短信发送客户端库,同时支持深源恒际、腾讯云和阿里云短信平台,提供单例模式和门面模式两种使用方式,包含完整的类型声明和异常处理机制。
本库采用模块化设计,按短信平台拆分实现,便于维护和扩展:
src/driver/AbstractSMS.php - 抽象基类,定义短信发送接口和通用功能src/SMS.php - 工厂类,负责创建和返回具体的短信平台实现实例src/driver/DeepfinchSMS.php - 深源恒际短信平台实现src/driver/TencentSMS.php - 腾讯云短信平台实现src/driver/AliyunSMS.php - 阿里云短信平台实现src/SMSFacade.php - 门面类,提供更简洁的API调用方式使用Composer安装:
composer require since/sms
$deepfinchConfig = [
'provider' => 'deepfinch', // 短信服务提供商
'accessKeyId' => 'your_access_key_id', // 深源恒际API ID
'accessKeySecret' => 'your_access_key_secret', // 深源恒际API密钥
'apiUrl' => 'https://cloudapi.deepfinch.com/data/send_sms', // 短信发送API地址
'timeout' => 5, // 请求超时时间(秒)
'templates' => [ // 模板配置
'verification' => 'your_verification_template_id', // 验证码模板ID
'notification' => 'your_notification_template_id', // 通知模板ID
],
];
$tencentConfig = [
'provider' => 'tencent', // 短信服务提供商
'accessKeyId' => 'your_tencent_access_key_id', // 腾讯云API密钥ID
'accessKeySecret' => 'your_tencent_access_key_secret', // 腾讯云API密钥
'timeout' => 5, // 请求超时时间(秒)
'templates' => [ // 模板配置
'verification' => 'your_tencent_verification_template_id', // 腾讯云模板ID
'notification' => 'your_tencent_notification_template_id', // 腾讯云模板ID
],
'tencent' => [ // 腾讯云特有配置
'sdkAppId' => 'your_tencent_sdk_app_id', // 腾讯云SDK AppID
'signName' => 'your_tencent_sign_name', // 腾讯云短信签名
'region' => 'ap-guangzhou', // 腾讯云地域,默认ap-guangzhou
],
];
$aliyunConfig = [
'provider' => 'aliyun', // 短信服务提供商
'accessKeyId' => 'your_aliyun_access_key_id', // 阿里云AccessKey ID
'accessKeySecret' => 'your_aliyun_access_key_secret', // 阿里云AccessKey Secret
'timeout' => 5, // 请求超时时间(秒)
'templates' => [ // 模板配置
'verification' => 'your_aliyun_verification_template_id', // 阿里云模板CODE
'notification' => 'your_aliyun_notification_template_id', // 阿里云模板CODE
],
'aliyun' => [ // 阿里云特有配置
'signName' => 'your_aliyun_sign_name', // 阿里云短信签名
'regionId' => 'cn-hangzhou', // 阿里云地域ID,默认cn-hangzhou
],
];
use since\SMSFacade;
// 初始化门面实例
$smsFacade = SMSFacade::getInstance($deepfinchConfig);
// 生成验证码
$verificationCode = rand(100000, 999999);
// 发送短信
$result = $smsFacade->send('13812345678', 'verification', [$verificationCode]);
// 查看结果
var_dump($result);
use since\SMSFacade;
// 初始化门面实例
$tencentSmsFacade = SMSFacade::getInstance($tencentConfig);
// 生成验证码
$verificationCode = rand(100000, 999999);
// 发送短信
$tencentResult = $tencentSmsFacade->send('13812345678', 'verification', [$verificationCode]);
// 查看结果
var_dump($tencentResult);
use since\SMSFacade;
// 初始化门面实例
$aliyunSmsFacade = SMSFacade::getInstance($aliyunConfig);
// 生成验证码
$verificationCode = rand(100000, 999999);
// 发送短信
$aliyunResult = $aliyunSmsFacade->send('13812345678', 'verification', [$verificationCode]);
// 查看结果
var_dump($aliyunResult);
use since\SMS;
// 初始化单例实例
$sms = SMS::getInstance($deepfinchConfig);
// 发送短信
$result = $sms->send('13812345678', 'verification', [$verificationCode]);
// 查看结果
var_dump($result);
use since\SMS;
// 初始化单例实例
$tencentSms = SMS::getInstance($tencentConfig);
// 发送短信
$tencentResult = $tencentSms->send('13812345678', 'verification', [$verificationCode]);
// 查看结果
var_dump($tencentResult);
use since\SMS;
// 初始化单例实例
$aliyunSms = SMS::getInstance($aliyunConfig);
// 发送短信
$aliyunResult = $aliyunSms->send('13812345678', 'verification', [$verificationCode]);
// 查看结果
var_dump($aliyunResult);
// 方式一:门面模式批量发送
$batchResult = SMSFacade::getInstance($deepfinchConfig)->batchSend(
['13812345678', '13987654321'],
'verification',
[$verificationCode]
);
// 方式二:单例模式批量发送
$batchResult = SMS::getInstance($deepfinchConfig)->batchSend(
['13812345678', '13987654321'],
'verification',
[$verificationCode]
);
// 查看结果
var_dump($batchResult);
// 方式一:门面模式批量发送
$tencentBatchResult = SMSFacade::getInstance($tencentConfig)->batchSend(
['13812345678', '13987654321'],
'verification',
[$verificationCode]
);
// 方式二:单例模式批量发送
$tencentBatchResult = SMS::getInstance($tencentConfig)->batchSend(
['13812345678', '13987654321'],
'verification',
[$verificationCode]
);
// 查看结果
var_dump($tencentBatchResult);
// 方式一:门面模式批量发送
$aliyunBatchResult = SMSFacade::getInstance($aliyunConfig)->batchSend(
['13812345678', '13987654321'],
'verification',
[$verificationCode]
);
// 方式二:单例模式批量发送
$aliyunBatchResult = SMS::getInstance($aliyunConfig)->batchSend(
['13812345678', '13987654321'],
'verification',
[$verificationCode]
);
// 查看结果
var_dump($aliyunBatchResult);
库会抛出标准的PHP Exception异常,包含错误信息,建议使用try/catch进行异常处理:
try {
$result = SMSFacade::getInstance($config)->send('13812345678', 'verification', [123456]);
// 处理成功结果
} catch (Exception $e) {
// 处理异常
echo '短信发送失败:' . $e->getMessage();
}
库会自动验证手机号格式:
短信发送成功后,返回API响应的JSON解析数组。发送失败时,返回false。
配置信息安全:建议将accessKeyId和accessKeySecret等敏感信息存储在环境变量或配置文件中,不要硬编码到代码里
错误重试机制:对于网络请求失败的情况,建议添加重试机制
频率控制:遵守短信发送频率限制,避免短时间内向同一手机号发送大量短信
异常处理:始终使用try/catch捕获可能的异常
请确保PHP版本 >= 8.0
请确保已安装curl和json扩展
安装依赖包:
composer install
php index.php
This project is licensed under the MIT License.
FAQs
一个基于PHP 8+的短信发送客户端库,采用驱动(driver)设计模式,提供单例模式和门面模式两种使用方式,支持配置管理、短信发送和批量发送功能,包含完整的类型声明和异常处理机制,易于扩展支持多种短信服务商。
We found that since/sms demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.

Product
Put vulnerability remediation on autopilot with automated security patches and intelligent dependency upgrades across your GitHub repositories.

Product
Socket Zero automates vulnerability remediation with upgrades and targeted patches, including fixes for 100% of known, patchable RubyGems CVEs.

Product
Explore Socket’s redesigned Alerts UI, multi-branch repository monitoring, and new tools for resolving findings, searching scans, and filtering security alerts.