$tel = ”133XXXXXXXX“;
$str = "01234567890";//短信验证码
$str = str_shuffle($str);
$code = substr($str, 0, 6);
$uri = ”http://dysmsapi.aliyuncs.com/?“;//阿里云短信接口地址
//构建请求参数
$params = [
'AccessKeyId' => ”XXXXXXXX“,//填写accesskeyid
'SignatureMethod' => 'HMAC-SHA1',
'SignatureNonce' => md5(time().rand(0,9999)),
'SignatureVersion' => '1.0',
'Timestamp' => gmdate("Y-m-d\TH:i:s\Z"),
'Format' => 'JSON',
'RegionId' => 'cn-hangzhou',
'Action' => 'SendSms',
'Version' => '2017-05-25',
'PhoneNumbers' => $tel,
'SignName' => ”“,//签名
'TemplateParam' => '{"code":"' . $code . '"}',//验证码
'TemplateCode' => ”XXXXXXXX“//模板名称
];
ksort($params);
$secretkey = ”XXXXXXXX“;//阿里云secretkey
$requestString = http_build_query($params);
//字符串替换
$requestString = str_replace('+','%20',$requestString);
$requestString = str_replace('*','%2A',$requestString);
$requestString = str_replace('%7E','~',$requestString);
$requestString = 'GET&%2F&'.urlencode($requestString);
//加签
$signature = base64_encode(hash_hmac('sha1', $requestString, $secretkey.'&', true));//加签结果
$req = $uri . 'Signature=' . $signature . '&' . http_build_query($params);//完整的api请求地址
网友评论