Python DEMO
功能說明:該接口要求提前在云片后臺添加模板,提交短信時,系統(tǒng)會自動匹配審核通過的模板,匹配成功任意一個模板即可發(fā)送。系統(tǒng)已提供的默認模板添加簽名后可以直接使用。
#!/usr/local/bin/python #-*-coding:utf-8-*- #author: jacky # Time: 15-12-15 # Desc: 短信http接口的python代碼調(diào)用示例 # http://www.ssjdyy2.com/api/demo.html # https訪問,需要安裝 openssl-devel庫。apt-get install openssl-devel import httplib import urllib import json #服務(wù)地址 sms_host = "sms.yunpian.com" voice_host = "voice.yunpian.com" #端口號 port = 443 #版本號 version = "v2" #查賬戶信息的URI user_get_uri = "/" + version + "/user/get.json" #智能匹配模板短信接口的URI sms_send_uri = "/" + version + "/sms/single_send.json" #模板短信接口的URI sms_tpl_send_uri = "/" + version + "/sms/tpl_single_send.json" #語音短信接口的URI sms_voice_send_uri = "/" + version + "/voice/send.json" #語音驗證碼 voiceCode = 1234 def 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_str def 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_str def 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_str def 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_str if __name__ == '__main__': #修改為您的apikey.可在官網(wǎng)(http://www.ssjdyy2.com)登錄后獲取 apikey = "xxxxxxxxxxxxxxxx" #修改為您要發(fā)送的手機號碼,多個號碼用逗號隔開 mobile = "xxxxxxxxxxxxxxxx" #修改為您要發(fā)送的短信內(nèi)容 text = "【云片網(wǎng)】您的驗證碼是1234" #查賬戶信息 print(get_user_info(apikey)) #調(diào)用智能匹配模板接口發(fā)短信 print send_sms(apikey,text,mobile) #調(diào)用模板接口發(fā)短信 tpl_id = 378965 #對應(yīng)的模板內(nèi)容為:【云片網(wǎng)】您的驗證碼是#code#,#min#分鐘內(nèi)有效。 tpl_value = {'#code#':'1234','#min#':'5'} print tpl_send_sms(apikey, tpl_id, tpl_value, mobile) #調(diào)用模板接口發(fā)語音短信 print send_voice_sms(apikey,voiceCode,mobile)