Ruby DEMO


功能说明:该接口要求提前在云片后台添加模板,提交短信时,系统会自动匹配审核通过的模板,匹配成功任意一个模板即可发送。系统已提供的默认模板添加签名后可以直接使用。


=begin

Desc:短信http接口的ruby代码调用示例
author shaoyan
date 2015-10.28

=end

require 'net/http'
require 'uri'
params = {}


#修改为您的apikey.可在官网(http://www.yunpian.com)登录后用户中心首页看到
apikey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
#修改为您要发送的手机号码,多个号码用逗号隔开
mobile = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
#修改为您要发送的短信内容
text = '【云片网】您的验证码是1234'
#查询账户信息HTTP地址
get_user_info_uri = URI.parse('https://sms.yunpian.com/v2/user/get.json')
#智能匹配模板发送HTTP地址
send_sms_uri = URI.parse('https://sms.yunpian.com/v2/sms/single_send.json')
#指定模板发送接口HTTP地址
send_tpl_sms_uri = URI.parse('https://sms.yunpian.com/v2/sms/tpl_single_send.json')
#发送语音验证码接口HTTP地址
send_voice_uri = URI.parse('https://voice.yunpian.com/v2/voice/send.json')

params['apikey'] = apikey
#打印用户信息
response =  Net::HTTP.post_form(get_user_info_uri,params)
print response.body + "\n"

params['mobile'] = mobile
params['text'] = text
#智能匹配模板发送
response = Net::HTTP.post_form(send_sms_uri,params)
print response.body + "\n"
#指定模板发送
#设置模板ID,如后台报备的模板ID为365741,模板内容为【云片网】您的验证码是#code#,#min#分钟内有效。
#设置对应的模板变量值

params['tpl_id'] = 365741
params['tpl_value'] = URI::escape('#code#') + '=' + URI::escape('1234') + '&' +
    URI::escape('#min#') + '=' + URI::escape('5')
response = Net::HTTP.post_form(send_tpl_sms_uri, params)

print response.body + "\n"
#发送语音验证码
params['code'] = 1234
response = Net::HTTP.post_form(send_voice_uri,params)
print response.body + "\n"