首先下载阿里云新版sdk
进入到项目根目录下,使用composer下载阿里云短信新版sdk
composer require alibabacloud/client
下载好后,就可以在thinkphp项目中用了
下面以实际案例来给大家演示
前端代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<p>手机号:<input type="text" id="phone"></p>
<p>验证码:<input type="text" id="code"><button id="btn">发送验证码</button></p>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8">
$('#btn').click(function(){
var phone = $('#phone').val();
if(phone==''){
alert('手机号不能为空');
return false;
}
$.post('/index.php/index/index/sendsms',{
'phone':phone
},function(data){
console.log(data);
if(data.status == 1){
alert('短信发送成功');
}else{
alert('短信发送失败');
}
},'json');
});
</script>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<p>手机号:<input type="text" id="phone"></p>
<p>验证码:<input type="text" id="code"><button id="btn">发送验证码</button></p>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8">
$('#btn').click(function(){
var phone = $('#phone').val();
if(phone==''){
alert('手机号不能为空');
return false;
}
$.post('/index.php/index/index/sendsms',{
'phone':phone
},function(data){
console.log(data);
if(data.status == 1){
alert('短信发送成功');
}else{
alert('短信发送失败');
}
},'json');
});
</script>
</body>
</html>
后端代码:
<?php
namespace app\index\controller;
use think\Controller;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
class Index extends Controller
{
public function index()
{
return $this->fetch();
}
/**
* 短信发送
* @param [number] $PhoneNumbers [手机号]
* @param [string] $SignName [短信签名名称]
* @param [string] $TemplateCode [短信模板ID]
* @param [json] $TemplateParam [模板内容参数] 如{"code":"1111"}
* @return [json] 成功返回 {"Message": "OK","RequestId": "A1FA52EF-10D4-432B-8AF3-C87A1F2F1E11","BizId": "636214959618469190^0","Code": "OK"}
*/
public function sendsms()
{
AlibabaCloud::accessKeyClient('你的accessKeyId', '你的accessSecret')->asDefaultClient();
$PhoneNumbers = input('post.phone');//接收传过来的手机号
$SignName = '志博网络';//短信签名
$TemplateCode = 'SMS_158380225';//短信模板ID
$code = rand(100000,999999);
$TemplateParam = json_encode(['code'=>$code]);//模板内容参数(格式为json)
try {
$result = AlibabaCloud::rpc()
->regionId('cn-hangzhou')
->product('Dysmsapi')
->version('2017-05-25')
->action('SendSms')
->method('POST')
->options([
'query' => [
'PhoneNumbers' => $PhoneNumbers,
'SignName' => $SignName,
'TemplateCode' => $TemplateCode,
'TemplateParam' => $TemplateParam,
],
])->request();
//成功返回json
$res = json_decode($result,true);
if($res['Code'] == 'OK' && $res['Message'] == 'OK'){
return ['status'=>1,'msg'=>'发送成功'];
}else{
return ['status'=>0,'msg'=>'发送失败'];
}
} catch (ClientException $e) {
return $e->getErrorMessage();
} catch (ServerException $e) {
return $e->getErrorMessage();
}
}
}
namespace app\index\controller;
use think\Controller;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
class Index extends Controller
{
public function index()
{
return $this->fetch();
}
/**
* 短信发送
* @param [number] $PhoneNumbers [手机号]
* @param [string] $SignName [短信签名名称]
* @param [string] $TemplateCode [短信模板ID]
* @param [json] $TemplateParam [模板内容参数] 如{"code":"1111"}
* @return [json] 成功返回 {"Message": "OK","RequestId": "A1FA52EF-10D4-432B-8AF3-C87A1F2F1E11","BizId": "636214959618469190^0","Code": "OK"}
*/
public function sendsms()
{
AlibabaCloud::accessKeyClient('你的accessKeyId', '你的accessSecret')->asDefaultClient();
$PhoneNumbers = input('post.phone');//接收传过来的手机号
$SignName = '志博网络';//短信签名
$TemplateCode = 'SMS_158380225';//短信模板ID
$code = rand(100000,999999);
$TemplateParam = json_encode(['code'=>$code]);//模板内容参数(格式为json)
try {
$result = AlibabaCloud::rpc()
->regionId('cn-hangzhou')
->product('Dysmsapi')
->version('2017-05-25')
->action('SendSms')
->method('POST')
->options([
'query' => [
'PhoneNumbers' => $PhoneNumbers,
'SignName' => $SignName,
'TemplateCode' => $TemplateCode,
'TemplateParam' => $TemplateParam,
],
])->request();
//成功返回json
$res = json_decode($result,true);
if($res['Code'] == 'OK' && $res['Message'] == 'OK'){
return ['status'=>1,'msg'=>'发送成功'];
}else{
return ['status'=>0,'msg'=>'发送失败'];
}
} catch (ClientException $e) {
return $e->getErrorMessage();
} catch (ServerException $e) {
return $e->getErrorMessage();
}
}
}
可以将上面的方法封装成一个短信发送类,方便我们在项目中多个地方调用
在application目录下建一个common文件夹,在common文件夹下建一个Alisms.php
<?php
namespace app\common;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
class Alisms
{
public static function sendsms($PhoneNumbers,$TemplateCode,$TemplateParam)
{
AlibabaCloud::accessKeyClient('你的accessKeyId', '你的accessSecret')->asDefaultClient();
$options = [
'query' => [
'PhoneNumbers' => $PhoneNumbers,
'SignName' => '你的短信签名',
'TemplateCode' => $TemplateCode,
'TemplateParam' => $TemplateParam,
],
];
try {
$result = AlibabaCloud::rpc()
->regionId('cn-hangzhou')
->product('Dysmsapi')
// ->scheme('https') // https | http
->version('2017-05-25')
->action('SendSms')
->method('POST')
->host('dysmsapi.aliyuncs.com')
->options($options)
->request();
return $result->toArray();
} catch (ClientException $e) {
return $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
return $e->getErrorMessage() . PHP_EOL;
}
}
}
namespace app\common;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
class Alisms
{
public static function sendsms($PhoneNumbers,$TemplateCode,$TemplateParam)
{
AlibabaCloud::accessKeyClient('你的accessKeyId', '你的accessSecret')->asDefaultClient();
$options = [
'query' => [
'PhoneNumbers' => $PhoneNumbers,
'SignName' => '你的短信签名',
'TemplateCode' => $TemplateCode,
'TemplateParam' => $TemplateParam,
],
];
try {
$result = AlibabaCloud::rpc()
->regionId('cn-hangzhou')
->product('Dysmsapi')
// ->scheme('https') // https | http
->version('2017-05-25')
->action('SendSms')
->method('POST')
->host('dysmsapi.aliyuncs.com')
->options($options)
->request();
return $result->toArray();
} catch (ClientException $e) {
return $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
return $e->getErrorMessage() . PHP_EOL;
}
}
}
这样我们需要发送短信的时候,就只要引入这个类,然后调用这个类中的sendsms方法发送短信