|
1 | 1 | # phpsms |
2 | | -sms send package for php |
| 2 | +自动负载均衡的短信发送php库 |
3 | 3 |
|
| 4 | +支持的服务商: |
| 5 | +| 服务商 | 模板短信 | 内容短信 | 语音验证码 | 最低消费 | 最低消费单价 | |
| 6 | +| ----- | :-----: | :-----: | :------: | :-------: | :-----: | |
| 7 | +| [Luosimao](http://luosimao.com) | no | yes | yes |¥850(1万条) |¥0.085/条| |
| 8 | +| [云片网络](http://www.yunpian.com) | no | yes | yes |¥55(1千条) |¥0.055/条| |
| 9 | +| [容联·云通讯](http://www.yuntongxun.com) | yes | no | yes |充值¥500 |¥0.055/条| |
| 10 | +| [SUBMAIL](http://submail.cn) | yes | no | no |¥100(1千条) |¥0.100/条| |
| 11 | +| [云之讯](http://www.ucpaas.com/) | yes | no | yes | |¥0.050/条| |
| 12 | + |
| 13 | +# 安装 |
| 14 | + |
| 15 | +```php |
| 16 | +composer require 'toplan/phpsms:~0.0.1' |
| 17 | +``` |
| 18 | + |
| 19 | +# 快速上手 |
| 20 | + |
| 21 | +###1. 配置 |
| 22 | + |
| 23 | +- 配置可用代理器 |
| 24 | + |
| 25 | + 见`config\phpsms.php` |
| 26 | + |
| 27 | +- 配置代理器所需参数 |
| 28 | + |
| 29 | + 见`config\agents.php` |
| 30 | + |
| 31 | +###2. Enjoy it! |
| 32 | + |
| 33 | +```php |
| 34 | +require('path/to/vendor/autoload.php'); |
| 35 | +use Toplan\PhpSms\Sms; |
| 36 | + |
| 37 | +Sms::make() |
| 38 | +//只希望使用模板方式发送短信,可以不设置内容content (如云通讯,Submail) |
| 39 | +Sms::make($tempId)->to('1828****349')->data(['12345', 5])->send(); |
| 40 | + |
| 41 | +//只希望使用内容方式放送,可以不设置模板id和模板数据data (如云片,luosimao) |
| 42 | +Sms::make()->to('1828****349')->content('【PhpSMS】亲爱的张三,欢迎访问,祝你工作愉快。')->send(); |
| 43 | + |
| 44 | +//同时确保能通过模板和内容方式发送。这样做的好处是,可以兼顾到各种代理器(服务商)! |
| 45 | +Sms::make([ |
| 46 | + 'YunTongXun' => '123', |
| 47 | + 'SubMail' => '123' |
| 48 | + ]) |
| 49 | + ->to('1828****349') |
| 50 | + ->data(['张三']) |
| 51 | + ->content('【签名】亲爱的张三,欢迎访问,祝你工作愉快。') |
| 52 | + ->send(); |
| 53 | +``` |
| 54 | + |
| 55 | +###4. 语法糖 |
| 56 | + |
| 57 | + * 发送给谁 |
| 58 | +```php |
| 59 | + $sms = $sms->to('1828*******'); |
| 60 | + $sms = $sms->to(['1828*******', '1828*******', ...]);//多个目标号码 |
| 61 | +``` |
| 62 | + |
| 63 | + * 设置模板ID |
| 64 | + |
| 65 | +如果你只想给第一个代理器设置模板ID, 你只需要传入一个id参数: |
| 66 | +```php |
| 67 | + //静态方法设置,并返回sms实例 |
| 68 | + $sms = Sms::make('20001'); |
| 69 | + //或 |
| 70 | + $sms = $sms->template('20001'); |
| 71 | +``` |
| 72 | + |
| 73 | +也可以这样设置: |
| 74 | +```php |
| 75 | + //静态方法设置,并返回sms实例 |
| 76 | + $sms = Sms::make(['YunTongXun' => '20001', 'SubMail' => 'xxx', ...]); |
| 77 | + //设置指定服务商的模板id |
| 78 | + $sms = $sms->template('YunTongXun', '20001')->template('SubMail' => 'xxx'); |
| 79 | + //一次性设置多个服务商的模板id |
| 80 | + $sms = $sms->template(['YunTongXun' => '20001', 'SubMail' => 'xxx', ...]); |
| 81 | +``` |
| 82 | + |
| 83 | + * 设置模板短信的模板数据 |
| 84 | +```php |
| 85 | + $sms = $sms->data([ |
| 86 | + 'code' => $code, |
| 87 | + 'minutes' => $minutes |
| 88 | + ]);//必须是数组 |
| 89 | +``` |
| 90 | + |
| 91 | + * 设置内容短信的内容 |
| 92 | + |
| 93 | + 有些服务商(如YunPian,Luosimao)只支持内容短信(即直接发送短信内容),不支持模板,那么就需要设置短信内容。 |
| 94 | +```php |
| 95 | + $sms = $sms->content('【签名】亲爱的张三,您的订单号是281xxxx,祝你购物愉快。'); |
| 96 | +``` |
| 97 | + |
| 98 | + * 发送短信 |
| 99 | +```php |
| 100 | + $results = $sms->send(); |
| 101 | +``` |
| 102 | + |
| 103 | + |
| 104 | +##自定义代理器 |
| 105 | + |
| 106 | +请注意命名规范,Foo为代理器(服务商)名称。配置项加入到config/agents.php中: |
| 107 | + |
| 108 | +```php |
| 109 | + 'Foo' => [ |
| 110 | + 'xxx' => 'some info', |
| 111 | + ... |
| 112 | + ] |
| 113 | +``` |
| 114 | + |
| 115 | +在agents目录下添加代理器类(注意类名为FooAgent),并继承Agent抽象类。如果使用到其他api,可以将api文件放入src/lib文件夹中。 |
| 116 | + |
| 117 | +```php |
| 118 | + namespace Toplan\Sms; |
| 119 | + class FooAgent extends Agent { |
| 120 | + //override |
| 121 | + //发送短信一级入口 |
| 122 | + public function sendSms($tempId, $to, Array $data, $content){ |
| 123 | + //在这个方法中调用二级入口 |
| 124 | + //根据你使用的服务商的接口选择调用哪个方式发送短信 |
| 125 | + $this->sendContentSms($to, $content); |
| 126 | + $this->sendTemplateSms($tempId, $to, Array $data); |
| 127 | + } |
| 128 | + |
| 129 | + //override |
| 130 | + //发送短信二级入口:发送内容短信 |
| 131 | + public function sendContentSms($to, $content) |
| 132 | + { |
| 133 | + //通过$this->config['key'],获取配置文件中的参数 |
| 134 | + $x = $this->config['xxx']; |
| 135 | + $x = $this->xxx;//也可以这样获取配置参数 |
| 136 | + //在这里实现发送内容短信,即直接发送内容 |
| 137 | + ... |
| 138 | + //切记将发送结果存入到$this->result |
| 139 | + $this->result['success'] = false;//是否发送成功 |
| 140 | + $this->result['info'] = $msg;//发送结果信息说明 |
| 141 | + $this->result['code'] = $code;//发送结果代码 |
| 142 | + } |
| 143 | + |
| 144 | + //override |
| 145 | + //发送短信二级入口:发送模板短信 |
| 146 | + public function sendTemplateSms($tempId, $to, Array $data) |
| 147 | + { |
| 148 | + //同上... |
| 149 | + } |
| 150 | + |
| 151 | + //override |
| 152 | + //发送语音验证码 |
| 153 | + public function voiceVerify($to, $code) |
| 154 | + { |
| 155 | + //同上... |
| 156 | + //与发送短信唯一不同的是,切记返回结果数组 |
| 157 | + return $this->result; |
| 158 | + } |
| 159 | + } |
| 160 | +``` |
| 161 | +至此, 新加代理器成功! |
| 162 | + |
| 163 | +##License |
| 164 | + |
| 165 | +MIT |
0 commit comments