Download the PHP package mikecai/php-sms without Composer
On this page you can find all versions of the php package mikecai/php-sms. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mikecai/php-sms
More information about mikecai/php-sms
Files in mikecai/php-sms
Package php-sms
Short Description Probably the most intelligent, elegant sms send library in php
License MIT
Informations about the package php-sms
PhpSms
可能是目前最聰明,優雅的PHP簡訊發送庫了。
phpsms的任務均衡調度功能由toplan/task-balancer提供。
特色
- 支持一般簡訊
- 支持簡訊發送狀態查詢
- 支持發送均衡調度,可按代理器權重值均衡選擇服務商發送。
- 支持一個或多個備用代理器(服務商)。
- 支持代理器計劃方案熱更新,可隨時更新/刪除/新加代理器。
- 允許推入變量,並自定義本質上實現邏輯(與所屬系統鬆散替換)。
- 靈活的發送前後鉤子。
- [自定義代理器](#自定義代理器)和[寄生代理器](#寄生代理器)。
服务商
服務商 | 模板簡訊 | 内容簡訊 | 語音驗證碼 | 最低消费 | 最低消费單價 |
---|---|---|---|---|---|
Every8D | × | √ | × | NT 8000(1萬條) | NT0.8/條 |
亞太企業簡訊(舊) | × | √ | × | (不提供) | (不提供) |
亞太企業簡訊(新-暫不提供) | × | √ | × | NT 8000(1萬條) | NT0.8/條 |
安装
开发中版本
快速上手
1. 配置
- 配置代理器所需參數
為你需要用到的簡訊服務商(即代理器)配置必要的參數。可以在config\phpsms.php
中鍵為agents
的數組中配置,也可以手動在程序中設置,示例如下:
- 配置代理器調度方案
可在config\phpsms.php
中鍵為scheme
的數組中配置。也可以手動在程序中設置,示例如下:
調度方案解析: 如果按照以上配置,那麼系統首次會嘗試使用
Every8d
或AptgXsms
發送簡訊,且它們被使用的概率分別為2/3
和1/3
。 如果使用其中一個代理器發送失敗,那麼會啟用備用代理器,按照配置可知備用代理器有SmsBao
和SmsBao
,那麼會依次調用直到發送成功或無備用代理器可用。 值得注意的是,如果首次嘗試的是AptgXsms
,那麼備用代理器將會只使用SmsBao
,也就是會排除使用過的代理器。
2. Enjoy it!
3. 在laravel和lumen中使用
-
服务提供器
- 生成配置文件
生成的配置文件為config/phpsms.php,然後在該文件中按提示配置。
- 使用
詳見API,示例:
API
API - 全局配置
Sms::scheme([$name[, $scheme]])
設置/獲取代理器的調度方案。
調度配置支持熱更新,即在應用系統的整個運行過程中都能隨時修改。
- 設置
手動設置代理器調度方案(優先級高於配置文件),如:
- 获取
通过该方法还能获取所有或指定代理器的调度方案,如:
scheme
静态方法的更多使用方法见高级调度配置
Sms::config([$name[, $config][, $override]]);
设置/获取代理器的配置数据。
参数配置支持热更新,即在应用系统的整个运行過程中都能随时修改。
- 設定
手动设置代理器的配置数据(优先级高于配置文件),如:
- 獲取
通過該方法還能獲取所有或指定代理器的配置參數,如:
Sms::beforeSend($handler[, $override]);
發送前鉤子,示例:
更多細節請查看 task-balancer 的
beforeRun
鉤子
Sms::beforeAgentSend($handler[, $override]);
代理器發送前鉤子,示例:
更多細節請查看 task-balancer 的
beforeDriverRun
鉤子
Sms::afterAgentSend($handler[, $override]);
代理器發送后鉤子,示例:
更多細節請查看 task-balancer 的
afterDriverRun
鉤子
Sms::afterSend($handler[, $override]);
發送後鉤子,示例:
更多細節請查看 task-balancer 的
afterRun
鉤子
Sms::queue([$enable[, $handler]])
該方法可以設置是否啟用鹼性以及定義如何推進到位數。
$handler
匿名函数可使用的参数:
$sms
: Sms实例$data
: Sms实例中的簡訊数据,等同于$sms->all()
定义如何推送到队列:
如果已經定義過如何推送到隊列,還可以繼續設置關閉/開啟隊列:
获取队列启用情况:
API - 發送相关
Sms::make()
生成發送簡訊的sms实例,并返回实例。
Sms::status()->query($batchId)->send();
查詢簡訊發送狀態
Sms::voice()
生成發送语音驗證碼的sms实例,并返回实例。
下方為大陸服務,參考就好
type($type)
设置实例类型,可选值有Sms::TYPE_SMS
和Sms::TYPE_VOICE(台灣簡訊沒支援),返回实例对象。
to($mobile)
设置發送给谁,并返回實例。
data($key, $value)
设置模板簡訊的模板数据,并返回实例对象。
通過
data
方法的組合除了可以實現模版簡訊的數據填充,還可以實現模版語音的數據填充。
content($text)
设置内容簡訊的内容,并返回实例对象。
一些内置的代理器(如SmsBao、YunPian、Luosimao)使用的是内容簡訊(即直接發送簡訊内容),那么就需要为它们设置簡訊内容。
params($agentName, $params)
直接设置参数到服务商提供的原生接口上,并返回实例对象。
all([$key])
获取Sms实例中的簡訊数据,不带参数时返回所有数据,其结构如下:
agent($name)
临时设置發送时使用的代理器(不会影响备用代理器的正常使用),并返回实例,$name
为代理器名称。
通过该方法设置的代理器将获得绝对优先权,但只对当前簡訊实例有效。
send()
请求發送簡訊/语音驗證碼。
$result
数据结构请参看task-balancer
自定义代理器
- step 1
可将配置项(如果有用到)加入到config/phpsms.php
中键为agents
的数组里。
- step 2
新建一个继承Toplan\PhpSms\Agent
抽象类的代理器类,建议代理器类名为FooAgent
,建议命名空间为Toplan\PhpSms
。
如果类名不为
FooAgent
或者命名空间不为Toplan\PhpSms
,在使用该代理器时则需要指定代理器类,详见高级调度配置。
- step 3
实现相应的接口,可选的接口有:
接口 | 说明 |
---|---|
ContentSms | 發送内容簡訊 |
TemplateSms | 發送模版簡訊 |
VoiceCode | 發送语音驗證碼 |
ContentVoice | 發送内容语音 |
TemplateVoice | 發送模版语音 |
FileVoice | 發送文件语音 |
高级调度配置
代理器的高级调度配置可以通过配置文件(config/phpsms.php
)中的scheme
项目配置,也可以通过scheme
静态方法设置。
值得注意的是,高级调度配置的值的数据结构是数组。
指定代理器类
如果你自定义了一个代理器,类名不为FooAgent
或者命名空间不为Toplan\PhpSms
,
那么你还可以在调度配置时指定你的代理器使用的类。
- 配置方式:
通过配置值中agentClass
键来指定类名。
- 示例:
寄生代理器
如果你既不想使用內置的代理器,也不想創建文件寫自定義代理器,那麼寄生代理器或許是個好的選擇, 無需定義代理器類,只需在調度配置時定義好發送簡訊和語音驗證碼的方式即可。
- 配置方式:
可以配置的發送過程有:
發送過程 | 参数列表 | 说明 |
---|---|---|
sendContentSms | $agent, $to, $content | 發送内容簡訊 |
sendTemplateSms | $agent, $to, $tmpId, $tmpData | 發送模版簡訊 |
sendVoiceCode | $agent, $to, $code | 發送语音驗證碼 |
sendContentVoice | $agent, $to, $content | 發送内容语音 |
sendTemplateVoice | $agent, $to, $tmpId, $tmpData | 發送模版语音 |
sendFileVoice | $agent, $to, $fileId | 發送文件语音 |
- 示例:
Todo
License
MIT
All versions of php-sms with dependencies
toplan/task-balancer Version ~0.5
jeremeamia/superclosure Version ~2.2
sabre/xml Version ^1.4.2
sabre/uri Version ^1.2.1