PHP code example of wenslim / sms
1. Go to this page and download the library: Download wenslim/sms library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wenslim / sms example snippets
$ composer
use Wenslim\Sms\Aliyun\Sms;
// 基础配置中获取的密钥
$accessKeyId = 'xxxxxxxxx';
$accessKeySecret = 'xxxxxxxxx';
// 初始化 Sms 类
$sms = new Sms($accessKeyId, $accessKeySecret);
// 接受人手机号
$sms -> setPhoneNumbers('xxxxxxxxxxx');
// 短信签名 & 短信模版CODE
$sms -> setSignName('xxx');
$sms -> setTemplateCode('xxx');
// 当模版设置变量
$sms -> setTemplateParam("{'code': '1234'}");
$sms -> send();
array(4) {
["Message"] => string(2) "OK"
["RequestId"] => string(36) "AE61E75C-6057-493A-989D-53DCF44C3686"
["BizId"] => string(20) "357420138207838299^0"
["Code"] => string(2) "OK"
}
use Wenslim\Sms\Txyun\Sms;
$appid = "xxxxxxxx";
$appkey = "xxxxxxxxxxxxxxxxxxxx";
$sms = new Sms($appid, $appkey);
// 国家码 & 手机号
$sms -> setMobile('86', '158xxxxxxxx');
// 模版 ID
$sms -> setTplId(xxxxxx);
// 模版参数
$sms -> setParams(["1234", "2"]);
$sms -> send();
array:4 [
"result" => 0
"errmsg" => "OK"
"sid" => "2019:-6287508033737713054"
"fee" => 1
]
.
.
.
'sms' => [
// 阿里云
'aliyun' => [
'accessKeyId' => env('SMS_ALIYUN_KEY'),
'accessKeySecret' => env('SMS_ALIYUN_SECRET'),
],
// 腾讯云
'txyun' => [
'appid' => env('SMS_TXYUN_APPID'),
'appkey' => env('SMS_TXYUN_APPKEY'),
]
],
.
.
.
// 阿里云
SMS_ALIYUN_KEY=xxxxxx
SMS_ALIYUN_SECRET=xxxxxx
// 腾讯云
SMS_TXYUN_APPID=xxxxxx
SMS_TXYUN_APPKEY=xxxxxx
use Wenslim\Sms\Aliyun\Sms;
class UserController extends Controller
{
public function send(Sms $sms)
{
$sms -> setPhoneNumbers('xxxxxx');
$sms -> setSignName('xxxxxx');
$sms -> setTemplateCode('xxxxxx');
$sms -> setTemplateParam("{'code': '1234'}");
$response = $sms -> send();
}
}
class UserController extends Controller
{
public function send()
{
$sms = app('sms');
$sms -> setSignName('xxxxxx');
$sms -> setTemplateCode('xxxxxx');
$sms -> setTemplateParam("{'code': '1234'}");
$response = $sms -> send();
}
}
use Wenslim\Sms\Txyun\Sms;
class UserController extends Controller
{
public function send(Sms $sms)
{
$sms -> setMobile('86', '158xxxxxxxx');
$sms -> setTplId(xxxxxx);
$sms -> setParams(["1234", "3"]);
$response = $sms -> send();
}
}
class UserController extends Controller
{
public function send()
{
$sms = app("tx_sms");
$sms -> setMobile('86', '158xxxxxxxx');
$sms -> setTplId(xxxxxx);
$sms -> setParams(["1234", "2"]);
$result = $sms -> send();
}
}