thinkphp5调用阿里大鱼短信

时间:2022-07-13 21:37:16
:在Controller.php
public function send()
{
if (request()->isPost()) {
$phone = input('post.phone/s');
if (!$phone) {
return ['code' => '-1', 'data' => '', 'msg' => '请输入手机号码'];
}
if (!preg_match("/^1[34578]{1}\d{9}$/", $phone)) {
return ['code' => '-1', 'data' => '', 'msg' => '请输入正确的手机号码'];
}
$code = $param['code'] = mt_rand(100000, 999999);
$param['product'] = 'abcdef';
Cache::set($phone, $code, 300);
$res = sendSms($phone, $param);
if ($res['code'] == 0) {
return ['code' => '0', 'data' => '', 'msg' => '发送成功'];
} else {
return ['code' => '-1', 'data' => '', 'msg' => '发送失败'];
}
}
}

  

:在Common.php
//引入  将阿里大鱼API文件放在extend目录下
use think\Loader;
use think\Config; function sendSms($mobile='', $param=[])
{
if(empty($mobile) || empty($param)){
return ['code' => -2, 'data' => '', 'msg' => '参数错误'];
}
Loader::import('Alidayu.Sms',EXTEND_PATH);
$appkey = Config::get('sms_appkey'); //在config.php文件里配置自己的sms_appkey
$secretKey = Config::get('sms_secretKey'); //在config.php文件里配置自己的sms_secretKey $sign = Config::get('sms_label');//在config.php文件里配置自己的sms_label
$sms = new \Sms($appkey,$secretKey);
$scenes_code = Config::get('scenes_code');//在config.php文件里配置自己的scenes_code $quind = date('YmdHis',time()) . mt_rand(1000,9999);
$response = $sms->sendSms(
$sign, // 短信签名
$scenes_code, // 短信模板编号
$mobile, // 短信接收者
$param, // 模板参数
$quind // 流水号
);
//返回结果
if($response->Code == 'OK'){
return ['code' => 0, 'data' => '', 'msg' => '发送成功'];
}
return ['code' => -1, 'data' => '', 'msg' => '发送失败']; }


调整后的Alidayu 点击API文件地址下载

thinkphp5调用阿里大鱼短信