国际版短信发送
服务器是国际版,短信发送也是国际版的
1、按照官方文档去安装一下,
composer require alibabacloud/client
结果出现下面报错提示: Could not find package alibabacloud/sdk.
2.解决方案:官方文档
composer config repo.packagist composer https://mirrors.aliyun.com/composer/
如果此种方式不可以用,使用其他阿里云 Composer 全量镜像
//文件头部引入一下哦
require_once(dirname(dirname(__FILE__))."/Alibabacloud/vendor/autoload.php");//要换成你的路径哦!
use AlibabaCloud\Client\AlibabaCloud;
/**
* 发送验证码
*
* @param int $mobile 手机号
* @param int $code 验证码,为空时将自动生成4位数字
* @param string $event 事件
* @return boolean
*/
public static function send($mobile, $code = null, $event = 'default')
{
$time = time() + 10*60; //10分钟之后过期
$ip = request()->ip();
// $result = Hook::listen('sms_send', $sms, null, true);
//执行发送操作
AlibabaCloud::accessKeyClient(self::$accessKeyId, self::$accessSecret)
->regionId('ap-southeast-1')
->asGlobalClient();
Db::startTrans();
try {
if ($event == 'pass'){
$msg = 'Your authentication information has passed, please log in to the APP to view';
}else if($event == 'refuse'){
$msg = 'Your authentication information has been rejected, please log in to the APP to view';
}else{ //验证码类
$msg = "Your verification code this time is: ".$code.", please do not disclose to others";
//存短信码
$sms = \app\common\model\Sms::create(['event' => $event, 'mobile' => $mobile, 'code' => $code, 'ip' => $ip, 'createtime' => $time]);
}
//执行发送操作
$result = AlibabaCloud::rpcRequest()
->product('Dysmsapi')
->host('dysmsapi.ap-southeast-1.aliyuncs.com')
->version('2018-05-01')
->action('SendMessageToGlobe')
->method('POST')
->options([
'query' => [
"To" => "86".$mobile,
"From" => "1234567890",//自定义短信来源(也就是发送方名称)
"Message" => $msg,
],
])
->request();
$result = $result->toArray();
if ($result['ResponseCode'] != 'OK') {
$sms->delete();
return false;
}
Db::commit();
}