阿里云短信代码

如果使用maven管理,引入aliyun-java-sdk-core-4.5.7.jar
如果不使用maven管理,引入aliyun-java-sdk-core-4.5.7.jar
opentracing-api-0.33.0.jar
opentracing-noop-0.33.0.jar
opentracing-util-0.33.0.jar

package com.frameboot.util.sms;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.io.IOException;
import java.util.Map;

/**
 * @author ywd
 * @create 2020-11-10 13:31
 */
public class SendSms {
//1.模板
    public static  String  verificationCode="SMS_111111";

    public static  String  payMessage="SMS_11111111";

    public static  String  confirmMessage="SMS_1111111";

    public static String send(String mobile, String conent,String templateCode) throws IOException {
    //2.传入自己id和秘钥
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "id", "secret");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", mobile);
        //3.修改签名
        request.putQueryParameter("SignName", "签名");
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", "{\"code\":\""+conent+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            Map map=  JSON.parseObject(response.getData(), Map.class);
            if( map.get("Code").equals("OK")){
                return  "200";
            }else{
                return  map.get("Code").toString();
            }
        } catch (ServerException e) {
            e.printStackTrace();
            return  "201";
        } catch (ClientException e) {
            e.printStackTrace();
            return  "201";
        }
    }

	//4.验证码不要直接传入111111,222222之类的信息,会被阿里云拦截
    public static void main(String[] args) {
        try {
            send("手机号","验证码",verificationCode);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 PHP 发送阿里云短信的示例代码: ```php // 替换成你的 AccessKeyId 和 AccessKeySecret $accessKeyId = "your_access_key_id"; $accessKeySecret = "your_access_key_secret"; // 签名和模板编号 $signName = "your_sign_name"; $templateCode = "your_template_code"; // 手机号码和验证码 $phoneNumbers = "18888888888"; $verifyCode = "123456"; // 构建请求参数 $params = array ( "code" => $verifyCode ); // 发起请求 $helper = new SmsHelper($accessKeyId, $accessKeySecret); $response = $helper->send($phoneNumbers, $signName, $templateCode, $params); // 处理响应结果 if ($response->Code == 'OK') { // 发送成功 } else { // 发送失败 } // SmsHelper 类定义 class SmsHelper { private $accessKeyId; private $accessKeySecret; private $serverUrl = "http://dysmsapi.aliyuncs.com"; public function __construct($accessKeyId, $accessKeySecret) { $this->accessKeyId = $accessKeyId; $this->accessKeySecret = $accessKeySecret; } public function send($phoneNumbers, $signName, $templateCode, $params) { $paramsJson = json_encode($params); $paramsArray = array ( "PhoneNumbers" => $phoneNumbers, "SignName" => $signName, "TemplateCode" => $templateCode, "TemplateParam" => $paramsJson, "RegionId" => "cn-hangzhou", "Action" => "SendSms", "Version" => "2017-05-25" ); $paramsString = http_build_query($paramsArray); $signature = $this->computeSignature($paramsArray); $url = "{$this->serverUrl}/?Signature={$signature}&{$paramsString}"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); curl_close($ch); return json_decode($response); } private function computeSignature($params) { ksort($params); $canonicalQueryString = http_build_query($params); $stringToSign = "GET&%2F&" . urlencode($canonicalQueryString); $signature = base64_encode(hash_hmac("sha1", $stringToSign, $this->accessKeySecret . "&", true)); return urlencode($signature); } } ``` 在上面的代码中,`SmsHelper` 类封装了发送短信的方法,其中 `send` 方法接受手机号码、签名、模板编号和消息参数作为参数,返回一个响应对象。你只需要将代码中的占位符替换成你自己的参数即可在你的应用程序中使用这个类发送短信
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值