php 短信验证 云之讯,python3.7实现云之讯、聚合短信平台的短信发送功能
1、云之訊平臺(tái)數(shù)據(jù)返回Json格式:
{'reason': '操作成功', 'result': {'sid': '17209241456456455454', 'fee': 1,
'count': 1}, 'error_code': 0}
2、聚合平臺(tái)數(shù)據(jù)返回Json格式:
{"code":"000000","count":"1","create_date":"2019-09-24
16:16:30","mobile":"187024545447","msg":"OK","smsid":"0f1245454545454
144545444","uid":""}
import time
from time import sleep
import sys
from datetime import datetime
import urllib.request
import json
from urllib import parse
import uuid
import http.client
#云之訊-短信請(qǐng)求url
sms_yzx_url = "https://open.ucpaas.com/ol/sms/sendsms"
#云之訊-賬號(hào)相關(guān)信息-應(yīng)用ID
sms_yzx_appid = "93649dcfexxxxxxxxxxxxxxxxxx"
#云之訊-賬號(hào)相關(guān)信息-用戶sid
sms_yzx_account_Sid = "eb6fxxxxxxxxxxxxxxxx"
#云之訊-賬號(hào)相關(guān)信息-密鑰
sms_yzx_auth_token = "027xxxxxxxxxxxxxxxx"
#云之訊-賬號(hào)相關(guān)信息-短信模板
sms_yzx_templateid = "475631"
#聚合數(shù)據(jù)-短信平臺(tái)-請(qǐng)求地址
sms_juhe_url = "http://v.juhe.cn/sms/send"
#聚合數(shù)據(jù)-短信平臺(tái)-短信模板
sms_juhe_templateid = "17488"
#聚合數(shù)據(jù)-短信平臺(tái)-密鑰
sms_juhe_auth_token = "40xxxxxxxxxxxxxxxx"
#短信服務(wù)平臺(tái)(0:云之訊短信 1: 聚合短信)
sms_service_choice = 0
date = datetime.now()
print(date.strftime("%Y-%m-%d %H:%M:%S"))
def juhe_sendsms(appkey, mobile, tpl_id, tpl_value):
sendurl = sms_juhe_url # 短信發(fā)送的URL,無(wú)需修改
params = 'key=%s&mobile=%s&tpl_id=%s&tpl_value=%s'%(appkey, mobile, tpl_id, parse.quote(tpl_value)) # 組合參數(shù)
wp = urllib.request.urlopen(sendurl + "?" + params)
content = wp.read() # 獲取接口返回內(nèi)容
result = json.loads(content)
# print(result)
if result:
error_code = result['error_code']
if error_code == 0:
# 發(fā)送成功
smsid = result['result']['sid']
print("sendsms success,smsid: %s" % (smsid) )
else:
# 發(fā)送失敗
print( "sendsms error :(%s), %s" % (error_code, result['reason']) )
else:
# 請(qǐng)求失敗
print( "request sendsms error" )
def yzx_sendsms(to, params, temp_id):
# @param to 手機(jī)號(hào)碼
# @param params 內(nèi)容數(shù)據(jù) 格式為數(shù)組 例如:{'12','34'},如不需替換請(qǐng)?zhí)?''
# @param temp_id 模板Id
data = {
"sid": sms_yzx_account_Sid,
"token": sms_yzx_auth_token,
"appid": sms_yzx_appid,
"templateid": temp_id,
"param": params,
"mobile": to,
}
# 將字典轉(zhuǎn)換為JSON字符串
json_data = json.dumps(data)
#print(json_data)
#發(fā)送請(qǐng)求頭
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json;charset=utf-8',
}
connect = http.client.HTTPConnection('open.ucpaas.com')
# 發(fā)送請(qǐng)求
connect.request(method='POST', url=sms_yzx_url, body=json_data, headers=headers)
# 獲取響應(yīng)
resp = connect.getresponse()
# print(resp)
# 響應(yīng)內(nèi)容
result = resp.read().decode('utf-8')
# print(result)
result = json.loads(result)
# 發(fā)送成功
# print(result)
# 如果發(fā)送短信成功,返回的字典數(shù)據(jù)中code字段的值為"000000"
if result["code"] == "000000":
# 返回0 表示發(fā)送短信成功
smsid = result['smsid']
print("sendsms success,smsid: %s, %s"% (smsid, result['msg']))
return 0
else:
# 返回-1 表示發(fā)送失敗
print("sendsms error :(%s), %s"% (result['code'], result['msg']))
return -1
def main():
mobile = '182xxxx' # 短信接受者的手機(jī)號(hào)碼
tpl_value = '#code#=4567'
juhe_sendsms(sms_juhe_auth_token, mobile, sms_juhe_templateid, tpl_value) # 請(qǐng)求發(fā)送短信
yzx_sendsms(mobile, ['1234'], sms_yzx_templateid)
if __name__ == '__main__':
main()
總結(jié)
以上所述是小編給大家介紹的python3.7實(shí)現(xiàn)云之訊、聚合短信平臺(tái)的短信發(fā)送功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
總結(jié)
以上是生活随笔為你收集整理的php 短信验证 云之讯,python3.7实现云之讯、聚合短信平台的短信发送功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 怎么爬before after之间的内容
- 下一篇: 艾宾浩斯遗忘曲线PHP,艾宾浩斯记忆遗忘