开发文档
VB
下面是 VBScript 示例,Windows 自带、无需安装。脚本文件请保存成 ANSI 编码(记事本“另存为”里把编码改成 ANSI),保存成 UTF-8 的话脚本里的中文会变成乱码。
通用识别(JSON / Base64)
Option Explicit
' 准快识别 VBScript 示例。保存为 predict.vbs,双击运行,或在命令行执行:cscript predict.vbs
' 文件编码请选 ANSI(记事本“另存为”里把编码改成 ANSI)。保存成 UTF-8 的话,脚本里的中文会变成乱码。
' 新版 Windows 正在逐步停用 VBScript,新项目建议直接用 VB.net 的示例。
Dim apiUrl, username, password, typeid, imagePath
apiUrl = "http://api.zhunkuai.cn/predict" ' 同时支持 HTTPS,把 http:// 换成 https:// 即可
username = "你的账号"
password = "你的密码"
' 识别类型,完整列表见“HTTP接口”文档。数英混合(默认):3
typeid = "3"
imagePath = "captcha.jpg" ' 待识别的图片,改成你自己的图片路径
' 请求体是 JSON:账号、密码要经过 JsonEscape 转义;image 只传图片内容的 base64,不要带 data:image/... 前缀
Dim body
body = "{""username"":""" & JsonEscape(username) & """,""password"":""" & JsonEscape(password) & """," & _
"""typeid"":""" & typeid & """,""image"":""" & ReadFileBase64(imagePath) & """}"
Dim http, response
Set http = CreateObject("MSXML2.ServerXMLHTTP.6.0")
' 四个超时依次是:域名解析、连接、发送、接收(毫秒)。文档要求 60 秒:太短会被主动断开,仍然扣费
http.setTimeouts 60000, 60000, 60000, 60000
http.open "POST", apiUrl, False
http.setRequestHeader "Content-Type", "application/json;charset=UTF-8"
http.send body
response = http.responseText
' 成功:{"success":true,"code":"0","message":"success","data":{"result":"hhum","id":"00504808..."}}
' 失败:{"success":false,"code":"-1","message":"失败原因","data":""}
' 注意:失败时 HTTP 状态码通常也是 200,成败只看 success 字段
If GetJsonValue(response, "success") = "true" Then
WScript.Echo "识别结果:" & GetJsonValue(response, "result")
' 识别结果不对时,用这个识别编号调“报错脚本”,会返还这次的名额或金额
WScript.Echo "识别编号:" & GetJsonValue(response, "id")
Else
WScript.Echo "识别失败:" & GetJsonValue(response, "message")
End If
' 读取图片文件并转成 base64
Function ReadFileBase64(path)
Dim stream, node
Set stream = CreateObject("ADODB.Stream")
stream.Type = 1 ' 1 = 二进制
stream.Open
stream.LoadFromFile path
' VBScript 自带的函数不能直接转 base64,这里借用 MSXML 的节点来转
Set node = CreateObject("MSXML2.DOMDocument.6.0").createElement("node")
node.dataType = "bin.base64"
node.nodeTypedValue = stream.Read
' 转出来的 base64 每 72 个字符会换一行,要把换行符去掉
ReadFileBase64 = Replace(node.text, vbLf, "")
stream.Close
End Function
' 把字符串里的反斜杠和双引号转义成 JSON 认可的写法
Function JsonEscape(s)
JsonEscape = Replace(Replace(s, "\", "\\"), """", "\""")
End Function
' 取出 JSON 里某个字段的值。VBScript 没有 JSON 库,这里用正则取最外层的那个同名字段
Function GetJsonValue(json, key)
Dim re, m
Set re = New RegExp
re.Pattern = """" & key & """\s*:\s*(?:""((?:[^""\\]|\\.)*)""|(true|false))"
Set m = re.Execute(json)
If m.Count = 0 Then
GetJsonValue = ""
ElseIf m(0).SubMatches(1) <> "" Then
GetJsonValue = m(0).SubMatches(1) ' true / false
Else
GetJsonValue = JsonUnescape(m(0).SubMatches(0))
End If
End Function
' 把 JSON 字符串里的转义还原:\" \\ \/ \n \t \r \uXXXX
Function JsonUnescape(s)
Dim out, i, c, n
out = ""
i = 1
Do While i <= Len(s)
c = Mid(s, i, 1)
If c <> "\" Then
out = out & c
i = i + 1
Else
n = Mid(s, i + 1, 1)
If n = "u" Then
out = out & ChrW(CLng("&H" & Mid(s, i + 2, 4)))
i = i + 6
ElseIf n = "n" Then
out = out & vbLf
i = i + 2
ElseIf n = "t" Then
out = out & vbTab
i = i + 2
ElseIf n = "r" Then
out = out & vbCr
i = i + 2
Else
out = out & n
i = i + 2
End If
End If
Loop
JsonUnescape = out
End Function
报错脚本
Option Explicit
' 报错脚本:识别结果不对时调用,会返还这次的名额或金额。只需要识别编号,不需要账号密码。
' 保存为 report.vbs 运行。文件编码请选 ANSI(记事本“另存为”里把编码改成 ANSI)。保存成 UTF-8 的话,脚本里的中文会变成乱码。
Dim apiUrl, recId, http, response
apiUrl = "http://api.zhunkuai.cn/reporterror.json"
recId = "识别接口返回的 data.id" ' 换成识别接口返回的识别编号
Set http = CreateObject("MSXML2.ServerXMLHTTP.6.0")
' 四个超时依次是:域名解析、连接、发送、接收(毫秒)
http.setTimeouts 60000, 60000, 60000, 60000
http.open "POST", apiUrl, False
http.setRequestHeader "Content-Type", "application/json;charset=UTF-8"
' 请求体是 JSON,识别编号只包含字母和数字,可以直接拼进去
http.send "{""id"":""" & recId & """}"
response = http.responseText
' 成功返回示例:{"success":true,"code":"0","message":"success","data":"报错成功"}
' 注意 data 是一段文字,不是对象;失败时原因在 message 里
WScript.Echo response
余额查询(GET)
Option Explicit
' 余额查询(GET 请求)。保存为 query.vbs 运行。文件编码请选 ANSI(记事本“另存为”里把编码改成 ANSI)。保存成 UTF-8 的话,脚本里的中文会变成乱码。
' 账号和密码是放在 URL 参数里传的,必须经过 UrlEncode 编码,否则密码里有 & + % # 或中文时会传错。
' 密码属于明文传输,建议把 apiUrl 开头的 http:// 换成 https://
Dim apiUrl, username, password, http, response
username = "你的账号"
password = "你的密码"
apiUrl = "http://api.zhunkuai.cn/queryAccountInfo.json?username=" & UrlEncode(username) & _
"&password=" & UrlEncode(password)
Set http = CreateObject("MSXML2.ServerXMLHTTP.6.0")
' 四个超时依次是:域名解析、连接、发送、接收(毫秒)
http.setTimeouts 60000, 60000, 60000, 60000
http.open "GET", apiUrl, False
http.send
response = http.responseText
' 成功返回示例:{"success":true,"code":"0","message":"success","data":{"balance":"0.156","consumed":"0.844","successNum":"557","failNum":"0"}}
' balance 实时余额
' consumed 实时总消费
' successNum 实时总识别成功的次数
' failNum 实时总识别错误数
' 失败时:success 为 false,原因在 message 里
WScript.Echo response
' 把文本做 URL 编码(按 UTF-8)。字母、数字和 - . _ ~ 保持原样,其他字符转成 %XX
Function UrlEncode(s)
If Len(s) = 0 Then
UrlEncode = ""
Exit Function
End If
Dim stream, bytes, i, b, out, start
Set stream = CreateObject("ADODB.Stream")
stream.Type = 2 ' 2 = 文本
stream.Charset = "utf-8"
stream.Open
stream.WriteText s
stream.Position = 0
stream.Type = 1 ' 切换成二进制后读出来的就是 UTF-8 字节
bytes = stream.Read
stream.Close
' ADODB.Stream 写入 UTF-8 文本时会在开头加 3 个字节的 BOM(EF BB BF),编码时要跳过
start = 1
' 注意:VBScript 里 And 的优先级比 = 高,每个比较都要单独加括号
If (AscB(MidB(bytes, 1, 1)) = 239) And (AscB(MidB(bytes, 2, 1)) = 187) And (AscB(MidB(bytes, 3, 1)) = 191) Then
start = 4
End If
out = ""
' 用 LenB / MidB / AscB 逐个取出字节,序号从 1 开始。
' 不能写成 bytes(i):在函数里用下标读取这种字节数组会报“类型不匹配”。
For i = start To LenB(bytes)
b = AscB(MidB(bytes, i, 1))
' 同样因为 And、Or 的优先级高于比较运算符,每个比较都加了括号
If ((b >= 48) And (b <= 57)) Or ((b >= 65) And (b <= 90)) Or ((b >= 97) And (b <= 122)) Or (b = 45) Or (b = 46) Or (b = 95) Or (b = 126) Then
out = out & Chr(b)
Else
out = out & "%" & Right("0" & Hex(b), 2)
End If
Next
UrlEncode = out
End Function