- 云片 API 文档
- 开发引导
- 使用场景
- 国内短信 API 列表
- 国际短信 API 列表
- 语音验证码
- 超级短信
- 账户信息
- 行为验证
- OTP
- 短链接工具
- 审核规则
- 文本短信
- 超级短信
- 返回码总体说明
- 云片 API 升级至 2.0 更强大 更安全
- 云片网络通信能力开放平台服务协议
- 其他
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
Python DEMO
功能说明:该接口要求提前在云片后台添加模板,提交短信时,系统会自动匹配审核通过的模板,匹配成功任意一个模板即可发送。系统已提供的默认模板添加签名后可以直接使用。
#!/usr/local/bin/python #-*-coding:utf-8-*- #author: jacky # Time: 15-12-15 # Desc: 短信http接口的python代码调用示例 # https://www.yunpian.com/api/demo.html # https访问,需要安装 openssl-devel库。apt-get install openssl-devel import httplib import urllib import json #服务地址 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): """ 通用接口发短信 """ 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): """ 模板接口发短信 """ 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): """ 通用接口发短信 """ 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.可在官网(http://www.yunpian.com)登录后获取 apikey = "xxxxxxxxxxxxxxxx" #修改为您要发送的手机号码,多个号码用逗号隔开 mobile = "xxxxxxxxxxxxxxxx" #修改为您要发送的短信内容 text = "【云片网】您的验证码是1234" #查账户信息 print(get_user_info(apikey)) #调用智能匹配模板接口发短信 print send_sms(apikey,text,mobile) #调用模板接口发短信 tpl_id = 378965 #对应的模板内容为:【云片网】您的验证码是#code#,#min#分钟内有效。 tpl_value = {'#code#':'1234','#min#':'5'} print tpl_send_sms(apikey, tpl_id, tpl_value, mobile) #调用模板接口发语音短信 print send_voice_sms(apikey,voiceCode,mobile)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论