使用API接口方式调用阿里云短信接口

传入手机号,AccessKey ID,Access Key Secret,签名名称,模版代码

date_default_timezone_set('GMT');
#手机号码
$phone = '';
#短信签名名称
$signName = '';
#AccessKeyID
$appid =  '';
#AccessKeySecret
$secret = ''; 
#模板code
$tplid = '';
$tplParam = json_encode(['code' => '33887u']);
$params = [
    'AccessKeyId' => $appid,
    'Timestamp'   => date('Y-m-d\TH:i:s\Z'),
    'SignatureMethod'=>'HMAC-SHA1',
    'SignatureVersion'=> '1.0',
    'SignatureNonce'  => uniqid(),//随机码
    //'Signature' => ''
    'Format'      => 'JSON',
    //业务参数
    'Action'       => 'SendSms',
    'Version'     => '2017-05-25',
    'RegionId'    => 'cn-hangzhou',
    'PhoneNumbers' => $phone,//接收号码
    'SignName'      => $signName,//签名
    'TemplateCode'  => $tplid,//模板id
    'TemplateParam' => $tplParam,

];
//排序
ksort($params);
//http_build_query($params)
$str = '';
foreach($params as $k => $v){
    $str .= urlencode($k) . '=' . urlencode($v) . '&';
}
$str = substr($str,0,-1);
$str = str_replace(['+','*','%7E'],['%20','%2A','~'],$str );
$new_str = 'GET&' . urlencode('/') . '&' . urlencode($str);
$sign = base64_encode(hash_hmac('sha1', $new_str, $secret . '&',true));
$sign = urlencode($sign);
$url = 'http://dysmsapi.aliyuncs.com/?Signature=' . $sign . '&'.  $str; 

 

你可能感兴趣的:(php)