This article mainly introduces Java's use of Yunpian API to send SMS verification codes. It mainly uses Java to implement SMS verification codes. Friends in need can refer to
. The following is an introduction to how to use machines to complete batch operations and automate SMS services.
Get APIKY
Yunpian.com provides a complete SDK and API, which can help developers quickly complete business development.
Before starting coding, you need to obtain APIKEY first, as shown below.
Get APIKY
After clicking the eye button, enter the verification code to view APIKY.
What needs to be explained here is that APIKEY is particularly important and must be protected to avoid leakage. Yunpian provides several protection mechanisms, such as verification, sensitive processing, independent sub-account APIKEY, etc. It can be seen that their security awareness is quite good.
Start Coding
With APIKY, you can start coding directly.
The most important interfaces are these three, single sending interface, batch sending interface, and batch personalized sending interface. After figuring out these three interfaces, other development is basically at your fingertips.
Single sending interface
* 单条短信发送,智能匹配短信模板 * * @param apikey 成功注册后登录云片官网,进入后台可查看 * @param text 需要使用已审核通过的模板或者默认模板 * @param mobile 接收的手机号,仅支持单号码发送 * @return json格式字符串 */ public static String singleSend(String apikey, String text, String mobile) { Map<String, String> params = new HashMap<String, String>();//请求参数集合 params.put("apikey", apikey); params.put("text", text); params.put("mobile", mobile); return post("https://sms.yunpian.com/v2/sms/single_send.json", params);//请自行使用post方式请求,可使用Apache HttpClient }
Among them, APIKEY is obtained from the cloud picture background.
For example, if I want to send a message to the number 1888888888, the code is as follows:
String apikey = fx33mio3561dah34jdk748vz9dkfjkd373zdfg28df43dfhjadj; String text = "[云片网] 您的短信验证码是 1234"; String mobile = 18888888888; testSendSms(apikey, text, mobile);
Such a text message will be sent. A detailed introduction can be found here.
Batch sending interface
You can also send text messages with the same content to multiple mobile phone numbers. The code is as follows:
/** * 批量发送短信,相同内容多个号码,智能匹配短信模板 * * @param apikey 成功注册后登录云片官网,进入后台可查看 * @param text 需要使用已审核通过的模板或者默认模板 * @param mobile 接收的手机号,多个手机号用英文逗号隔开 * @return json格式字符串 */ public static String batchSend(String apikey, String text, String mobile) { Map<String, String> params = new HashMap<String, String>();//请求参数集合 params.put("apikey", apikey); params.put("text", text); params.put("mobile", mobile); return post("https://sms.yunpian.com/v2/sms/batch_send.json", params);//请自行使用post方式请求,可使用Apache HttpClient }
Use commas to separate multiple mobile phone numbers. Mobile can be assigned the value
String mobile = "1888888888, 1234567890, 9876543210"
. For detailed instructions, please see here.
Batch personalized sending interface
Many times, the needs we encounter are not that simple. We often need to send different content to different mobile phone numbers, and Yunpian also provides the corresponding API. .
/** * 批量个性化发送 */ public static String multiSend() throws UnsupportedEncodingException { Map<String, String> params = new HashMap<String, String>();//请求参数集合 params.put("apikey", "your apikey"); params.put("text", java.net.URLEncoder.encode("【云片网】您的验证码是1234", "UTF-8") + "," + java.net.URLEncoder.encode("【云片网】您的登录码是8888", "UTF-8")); params.put("mobile", "13812345678,18888888888"); return post("https://sms.yunpian.com/v2/sms/multi_send.json", params);//请自行使用post方式请求,可使用Apache HttpClient }
The result of the above code is that the first text message "[Yunpian.com] Your verification code is 1234" was sent to 13812345678, and the second text message was sent to 13812345678. A text message "[Yunpian.com] Your login code is 8888" was sent to 18888888888. Of course, this implementation still looks stupid, but don't worry, Yunpian.com also provides variable templates. Just replace the corresponding ones in the template. In the variable part, you can easily send different text messages to multiple numbers. We can talk more about the variable template in the future. Anyway, it perfectly implements the OCP principle in the design pattern, which is simply classic.
For a more detailed introduction, you can see here.
The above is the detailed content of Detailed code explanation of using Java API to send SMS verification code. For more information, please follow other related articles on the PHP Chinese website!