国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

Java PHP Python實(shí)現(xiàn)短信驗(yàn)證碼和國(guó)際短信群發(fā)功能

2019-11-09 17:41:54
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

最近由于公司的業(yè)務(wù)拓展,需要給國(guó)外用戶發(fā)送國(guó)際短信,像西班牙、葡萄牙、意大利這些國(guó)家都要發(fā),還有中國(guó)的香港、澳門、臺(tái)灣(港澳臺(tái))這些地區(qū)也要發(fā),不過(guò)現(xiàn)在已經(jīng)有許多公司提供國(guó)際短信的業(yè)務(wù)了,之前使用過(guò)云片的驗(yàn)證碼業(yè)務(wù),順便看到他們也有國(guó)際短信的業(yè)務(wù),并且更重要的是,不需要修改任何代碼,只要添加下國(guó)際短信模板,就可以直接使用之前的代碼繼續(xù)發(fā)送國(guó)際短信,簡(jiǎn)直太方便了。

廢話不多說(shuō),直接上代碼。

java

/*** Created by bingone on 15/12/16.*/import org.apache.http.HttpEntity;import org.apache.http.NameValuePair;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.CloseableHttPResponse;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;import java.io.IOException;import java.net.URISyntaxException;import java.net.URLEncoder;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/*** 短信http接口的java代碼調(diào)用示例* 基于Apache HttpClient 4.3** @author songchao* @since 2015-04-03*/public class JavaSmsApi { //查賬戶信息的http地址 private static String URI_GET_USER_INFO = "https://sms.yunpian.com/v2/user/get.json"; //智能匹配模板發(fā)送接口的http地址 private static String URI_SEND_SMS = "https://sms.yunpian.com/v2/sms/single_send.json"; //模板發(fā)送接口的http地址 private static String URI_TPL_SEND_SMS = "https://sms.yunpian.com/v2/sms/tpl_single_send.json"; //發(fā)送語(yǔ)音驗(yàn)證碼接口的http地址 private static String URI_SEND_VOICE = "https://voice.yunpian.com/v2/voice/send.json"; //綁定主叫、被叫關(guān)系的接口http地址 private static String URI_SEND_BIND = "https://call.yunpian.com/v2/call/bind.json"; //解綁主叫、被叫關(guān)系的接口http地址 private static String URI_SEND_UNBIND = "https://call.yunpian.com/v2/call/unbind.json"; //編碼格式。發(fā)送編碼格式統(tǒng)一用UTF-8 private static String ENCODING = "UTF-8"; public static void main(String[] args) throws IOException, URISyntaxException { //修改為您的apikey.apikey可在官網(wǎng)(http://www.yunpian.com)登錄后獲取 String apikey = "xxxxxxxxxxxxxxxxxxxxx"; //修改為您要發(fā)送的手機(jī)號(hào) String mobile = "130xxxxxxxx"; /**************** 查賬戶信息調(diào)用示例 *****************/ System.out.println(JavaSmsApi.getUserInfo(apikey)); /**************** 使用智能匹配模板接口發(fā)短信(推薦) *****************/ //設(shè)置您要發(fā)送的內(nèi)容(內(nèi)容必須和某個(gè)模板匹配。以下例子匹配的是系統(tǒng)提供的1號(hào)模板) String text = "【云片網(wǎng)】您的驗(yàn)證碼是1234"; //發(fā)短信調(diào)用示例 // System.out.println(JavaSmsApi.sendSms(apikey, text, mobile)); /**************** 使用指定模板接口發(fā)短信(不推薦,建議使用智能匹配模板接口) *****************/ //設(shè)置模板ID,如使用1號(hào)模板:【#company#】您的驗(yàn)證碼是#code# long tpl_id = 1; //設(shè)置對(duì)應(yīng)的模板變量值 String tpl_value = URLEncoder.encode("#code#",ENCODING) +"=" + URLEncoder.encode("1234", ENCODING) + "&" + URLEncoder.encode("#company#",ENCODING) + "=" + URLEncoder.encode("云片網(wǎng)",ENCODING); //模板發(fā)送的調(diào)用示例 System.out.println(tpl_value); System.out.println(JavaSmsApi.tplSendSms(apikey, tpl_id, tpl_value, mobile)); /**************** 使用接口發(fā)語(yǔ)音驗(yàn)證碼 *****************/ String code = "1234"; //System.out.println(JavaSmsApi.sendVoice(apikey, mobile ,code)); /**************** 使用接口綁定主被叫號(hào)碼 *****************/ String from = "+86130xxxxxxxx"; String to = "+86131xxxxxxxx"; Integer duration = 30*60;// 綁定30分鐘 // System.out.println(JavaSmsApi.bindCall(apikey, from ,to , duration)); /**************** 使用接口解綁主被叫號(hào)碼 *****************/ // System.out.println(JavaSmsApi.unbindCall(apikey, from, to)); } /** * 取賬戶信息 * * @return json格式字符串 * @throws java.io.IOException */ public static String getUserInfo(String apikey) throws IOException, URISyntaxException { Map<String, String> params = new HashMap<String, String>(); params.put("apikey", apikey); return post(URI_GET_USER_INFO, params); } /** * 智能匹配模板接口發(fā)短信 * * @param apikey apikey * @param text  短信內(nèi)容 * @param mobile  接受的手機(jī)號(hào) * @return json格式字符串 * @throws IOException */ public static String sendSms(String apikey, String text, String mobile) throws IOException { Map<String, String> params = new HashMap<String, String>(); params.put("apikey", apikey); params.put("text", text); params.put("mobile", mobile); return post(URI_SEND_SMS, params); } /** * 通過(guò)模板發(fā)送短信(不推薦) * * @param apikey apikey * @param tpl_id  模板id * @param tpl_value  模板變量值 * @param mobile  接受的手機(jī)號(hào) * @return json格式字符串 * @throws IOException */ public static String tplSendSms(String apikey, long tpl_id, String tpl_value, String mobile) throws IOException { Map<String, String> params = new HashMap<String, String>(); params.put("apikey", apikey); params.put("tpl_id", String.valueOf(tpl_id)); params.put("tpl_value", tpl_value); params.put("mobile", mobile); return post(URI_TPL_SEND_SMS, params); } /** * 通過(guò)接口發(fā)送語(yǔ)音驗(yàn)證碼 * @param apikey apikey * @param mobile 接收的手機(jī)號(hào) * @param code 驗(yàn)證碼 * @return */ public static String sendVoice(String apikey, String mobile, String code) { Map<String, String> params = new HashMap<String, String>(); params.put("apikey", apikey); params.put("mobile", mobile); params.put("code", code); return post(URI_SEND_VOICE, params); } /** * 通過(guò)接口綁定主被叫號(hào)碼 * @param apikey apikey * @param from 主叫 * @param to 被叫 * @param duration 有效時(shí)長(zhǎng),單位:秒 * @return */ public static String bindCall(String apikey, String from, String to , Integer duration ) { Map<String, String> params = new HashMap<String, String>(); params.put("apikey", apikey); params.put("from", from); params.put("to", to); params.put("duration", String.valueOf(duration)); return post(URI_SEND_BIND, params); } /** * 通過(guò)接口解綁綁定主被叫號(hào)碼 * @param apikey apikey * @param from 主叫 * @param to 被叫 * @return */ public static String unbindCall(String apikey, String from, String to) { Map<String, String> params = new HashMap<String, String>(); params.put("apikey", apikey); params.put("from", from); params.put("to", to); return post(URI_SEND_UNBIND, params); } /** * 基于HttpClient 4.3的通用POST方法 * * @param url 提交的URL * @param paramsMap 提交<參數(shù),值>Map * @return 提交響應(yīng) */ public static String post(String url, Map<String, String> paramsMap) { CloseableHttpClient client = HttpClients.createDefault(); String responseText = ""; CloseableHttpResponse response = null; try { HttpPost method = new HttpPost(url); if (paramsMap != null) { List<NameValuePair> paramList = new ArrayList<NameValuePair>(); for (Map.Entry<String, String> param : paramsMap.entrySet()) { NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue()); paramList.add(pair); } method.setEntity(new UrlEncodedFormEntity(paramList, ENCODING)); } response = client.execute(method); HttpEntity entity = response.getEntity(); if (entity != null) { responseText = EntityUtils.toString(entity, ENCODING); } } catch (Exception e) { e.printStackTrace(); } finally { try { response.close(); } catch (Exception e) { e.printStackTrace(); } } return responseText; }}

php

<?phpheader("Content-Type:text/html;charset=utf-8");$apikey = "xxxxxxxxxxx"; //修改為您的apikey(https://www.yunpian.com)登錄官網(wǎng)后獲取$mobile = "xxxxxxxxxxx"; //請(qǐng)用自己的手機(jī)號(hào)代替$text="【云片網(wǎng)】您的驗(yàn)證碼是1234";$ch = curl_init();/* 設(shè)置驗(yàn)證方式 */curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded','charset=utf-8'));/* 設(shè)置返回結(jié)果為流 */curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);/* 設(shè)置超時(shí)時(shí)間*/curl_setopt($ch, CURLOPT_TIMEOUT, 10);/* 設(shè)置通信方式 */curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 取得用戶信息$json_data = get_user($ch,$apikey);$array = json_decode($json_data,true);echo '<pre>';print_r($array);// 發(fā)送短信$data=array('text'=>$text,'apikey'=>$apikey,'mobile'=>$mobile);$json_data = send($ch,$data);$array = json_decode($json_data,true);echo '<pre>';print_r($array);// 發(fā)送模板短信// 需要對(duì)value進(jìn)行編碼$data=array('tpl_id'=>'1','tpl_value'=>('#code#').'='.urlencode('1234').'&'.urlencode('#company#').'='.urlencode('歡樂(lè)行'),'apikey'=>$apikey,'mobile'=>$mobile);print_r ($data);$json_data = tpl_send($ch,$data);$array = json_decode($json_data,true);echo '<pre>';print_r($array);// 發(fā)送語(yǔ)音驗(yàn)證碼$data=array('code'=>'9876','apikey'=>$apikey,'mobile'=>$mobile);$json_data =voice_send($ch,$data);$array = json_decode($json_data,true);echo '<pre>';print_r($array);curl_close($ch);/***************************************************************************************///獲得賬戶function get_user($ch,$apikey){ curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/user/get.json'); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('apikey' => $apikey))); return curl_exec($ch);}function send($ch,$data){ curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json'); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); return curl_exec($ch);}function tpl_send($ch,$data){ curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/tpl_single_send.json'); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); return curl_exec($ch);}function voice_send($ch,$data){ curl_setopt ($ch, CURLOPT_URL, 'http://voice.yunpian.com/v2/voice/send.json'); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); return curl_exec($ch);}?>

Python

#!/usr/local/bin/python#-*-coding:utf-8-*-#author: jacky# Time: 15-12-15# Desc: 短信http接口的python代碼調(diào)用示例# https://www.yunpian.com/api/demo.html# https訪問(wèn),需要安裝 openssl-devel庫(kù)。apt-get install openssl-develimport httplibimport urllibimport json#服務(wù)地址sms_host = "sms.yunpian.com"voice_host = "voice.yunpian.com"#端口號(hào)port = 443#版本號(hào)version = "v2"#查賬戶信息的URIuser_get_uri = "/" + version + "/user/get.json"#智能匹配模板短信接口的URIsms_send_uri = "/" + version + "/sms/single_send.json"#模板短信接口的URIsms_tpl_send_uri = "/" + version + "/sms/tpl_single_send.json"#語(yǔ)音短信接口的URIsms_voice_send_uri = "/" + version + "/voice/send.json"#語(yǔ)音驗(yàn)證碼voiceCode = 1234def get_user_info(apikey): """ 取賬戶信息 """ conn = httplib.HTTPSConnection(sms_host , port=port) headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} conn.request('POST',user_get_uri,urllib.urlencode( {'apikey' : apikey})) response = conn.getresponse() response_str = response.read() conn.close() return response_strdef send_sms(apikey, text, mobile): """ 通用接口發(fā)短信 """ params = urllib.urlencode({'apikey': apikey, 'text': text, 'mobile':mobile}) headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} conn = httplib.HTTPSConnection(sms_host, port=port, timeout=30) conn.request("POST", sms_send_uri, params, headers) response = conn.getresponse() response_str = response.read() conn.close() return response_strdef tpl_send_sms(apikey, tpl_id, tpl_value, mobile): """ 模板接口發(fā)短信 """ params = urllib.urlencode({'apikey': apikey, 'tpl_id':tpl_id, 'tpl_value': urllib.urlencode(tpl_value), 'mobile':mobile}) headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} conn = httplib.HTTPSConnection(sms_host, port=port, timeout=30) conn.request("POST", sms_tpl_send_uri, params, headers) response = conn.getresponse() response_str = response.read() conn.close() return response_strdef send_voice_sms(apikey, code, mobile): """ 通用接口發(fā)短信 """ params = urllib.urlencode({'apikey': apikey, 'code': code, 'mobile':mobile}) headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} conn = httplib.HTTPSConnection(voice_host, port=port, timeout=30) conn.request("POST", sms_voice_send_uri, params, headers) response = conn.getresponse() response_str = response.read() conn.close() return response_strif __name__ == '__main__': #修改為您的apikey.可在官網(wǎng)(http://www.yunpian.com)登錄后獲取 apikey = "xxxxxxxxxxxxxxxx" #修改為您要發(fā)送的手機(jī)號(hào)碼,多個(gè)號(hào)碼用逗號(hào)隔開 mobile = "xxxxxxxxxxxxxxxx" #修改為您要發(fā)送的短信內(nèi)容 text = "【云片網(wǎng)】您的驗(yàn)證碼是1234" #查賬戶信息 print(get_user_info(apikey)) #調(diào)用智能匹配模板接口發(fā)短信 print send_sms(apikey,text,mobile) #調(diào)用模板接口發(fā)短信 tpl_id = 1 #對(duì)應(yīng)的模板內(nèi)容為:您的驗(yàn)證碼是#code#【#company#】 tpl_value = {'#code#':'1234','#company#':'云片網(wǎng)'} print tpl_send_sms(apikey, tpl_id, tpl_value, mobile) #調(diào)用模板接口發(fā)語(yǔ)音短信 print send_voice_sms(apikey,voiceCode,mobile)

代碼看上去有點(diǎn)亂了,不過(guò)我們用到的API接口也就那么幾個(gè),具體的可以看這篇文章如何使用云片API發(fā)送短信驗(yàn)證碼,只要把那三個(gè)接口搞定了,無(wú)論是國(guó)際短信、國(guó)內(nèi)短信還是短信驗(yàn)證碼、手機(jī)驗(yàn)證碼,都可以輕松搞定,so easy!


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 丘北县| 青冈县| 荆门市| 库尔勒市| 宾川县| 偏关县| 囊谦县| 岳池县| 新晃| 高密市| 栾城县| 阿拉善右旗| 定南县| 光泽县| 辽阳县| 沅陵县| 建瓯市| 桑植县| 丹东市| 昌都县| 游戏| 郯城县| 拉萨市| 余庆县| 清水河县| 通化市| 晋中市| 乌拉特中旗| 永寿县| 旬阳县| 西贡区| 龙游县| 兰西县| 西吉县| 分宜县| 安乡县| 会昌县| 盘山县| 孟村| 东乡| 安宁市|