阿里云短信服务实现短信验证功能
首先申请一个阿里云的账户,登陆后访问以下网址
https://help.aliyun.com/product/44282.html
由于阿里云没有给测试的条数,大家可以买一个9.9的400条的新人套餐服务
接下来访问国内消息选项创建签名和模板,此处需要单位的营业执照和授权书,如果没有的可以试着尝试腾讯云,用自己的小程序注册,也可实现短信验证码,有100条免费测试条数https://blog.****.net/qq_29099209/article/details/87632159
然后访问https://help.aliyun.com/document_detail/55451.html?spm=a2c4g.11186623.6.611.1c4c38d7MYx0MD查看JAVA,PHP,Python,JavaScript,C#如何调用的,我使用的是PHP,这上面也有参数格式,可以参考我的代码
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
class AlibabaController extends Controller
{
public function AlibabaSms($number)
{
Log::info(333333333333);
Log::info($number);
AlibabaCloud::accessKeyClient('LTAxxxxxxxxxx', 'j3hwxxxxxxxxxxxxx')
->regionId('cn-zhengzhou')// replace regionId as you need
->asGlobalClient();
$TemplateCode = $this->getRandomString(4);
try {
$result = AlibabaCloud::rpcRequest()
->product('Dysmsapi')
// ->scheme('https') // https | http
->version('2017-05-25')
->action('SendSms')
->method('POST')
->options([
'query' => [
'RegionId' => 'cn-hangzhou',
'PhoneNumbers' => 'xxxxx',发送的电话号,多个以逗号分隔
'SignName' => 'xxxxx',签名的名称
'TemplateCode' => 'xxxx',模板代码
'TemplateParam' => '{"code":"'.$TemplateCode.'"}',发送的短信参数
],
])
->request();
} catch (ClientException $e) {
echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
echo $e->getErrorMessage() . PHP_EOL;
}
}
//生成校验码
public function getRandomString($len, $chars = null)
{
if (is_null($chars)) {
$chars = "0123456789";
}
mt_srand(10000000 * (double)microtime());
for ($i = 0, $str = '', $lc = strlen($chars) - 1; $i < $len; $i++) {
$str .= $chars[mt_rand(0, $lc)];
}
return $str;
}
}