1. 程式人生 > 其它 >阿里雲簡訊驗證碼API介面

阿里雲簡訊驗證碼API介面

阿里雲簡訊驗證碼介面網站:https://market.aliyun.com/products/57124001/cmapi00037170.html?spm=5176.2020520132.101.20.1e047218HnYIPU#sku=yuncode3117000001

請求引數:

隨機四位數驗證碼原始碼

import urllib, sys
import ssl
import urllib.request as urllib2
import random

def messageAPI(phone):
    host = 'https://dfsns.market.alicloudapi.com'
    path = '
/data/send_sms' method = 'POST' appcode = '這裡填你的code,購買後會有' querys = '' bodys = {} url = host + path # 隨機生成四位數驗證碼 code=random.randint(1000,9999) bodys['content'] = '''code:{},expire_at:5'''.format(code) bodys['phone_number'] = phone bodys['template_id'] = '''TPL_0001'''
post_data = urllib.parse.urlencode(bodys).encode("utf-8") request = urllib2.Request(url, post_data) request.add_header('Authorization', 'APPCODE ' + appcode) # //根據API的要求,定義相對應的Content-Type request.add_header('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8') ctx
= ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE response = urllib2.urlopen(request, context=ctx) content = response.read() if (content): print("傳送手機號為:{},驗證碼為:{}".format(phone,code)) return phone,code