阿里大于是阿里通信旗下产品,融合了三大运营商的通信能力,提供包括短信、语音、流量直充、私密专线、店铺手机号等个性化服务。每条四毛五,价钱还算公道,经老农测试,响应速度非常快,基本上是秒到。官方文档提供了以下语言的 Demo
- JAVA
- .NET
- PHP
- Python
- CURL
- C/C++
- NodeJS
唯独没有 Dephi,这不能怪马云,毕竟 Delphi 实在太小众了。
最近用 Delphi 写个 App,注册用户需要用到手机短信验证,于是找到的阿里大于,使用 Delphi 10.1 berlin 写了个简单的 Demo 并测试通过,现在交出代码:
1 /// <author>全能地图(QQ:64445322)</author> 2 /// <summary> 3 /// 利用阿里大于接口发短信 4 /// 阿里大于网址:http://www.alidayu.com 5 /// 阿里大于短信接口文档:https://api.alidayu.com/doc2/apiDetail.htm?apiId=25450 6 /// </summary> 7 /// <param name="AppKey">TOP分配给应用的AppKey</param> 8 /// <param name="AppSecret">AppSecret</param> 9 /// <param name="ReceiveNumber">接收手机号码</param> 10 /// <param name="FreeSignName">短信签名,传入的短信签名必须是在阿里大于“管理中心-短信签名管理”中的可用签名</param> 11 /// <param name="TemplateCode">短信模板ID</param> 12 /// <param name="TemplateContent">短信模板变量,例如:{"code":"1234","product":"alidayu"}</param> 13 /// <param name="ResultMsg">下发结果消息</param> 14 /// <returns>是否成功,True = 成功 ,false = 失败</returns> 15 function SendSMS(const AppKey, AppSecret, ReceiveNumber, FreeSignName, TemplateCode, TemplateContent: string; var ResultMsg: string): Boolean; 16 17 // 签名算法:http://open.taobao.com/doc2/detail.htm?articleId=101617&docType=1&treeId=1 18 function MakeSign(const AParams: TStringList; const AppSecret: string): string; 19 var 20 I: Integer; 21 Data: string; 22 begin 23 // 参数排序 24 AParams.Sort; 25 26 // 参数拼接 27 Data := ‘‘; 28 for I := 0 to AParams.Count - 1 do 29 Data := Data + AParams[I].Replace(‘=‘, ‘‘); 30 31 // HMAC 算法 32 Result := THashMD5.GetHMAC(Data, AppSecret).ToUpper; 33 end; 34 35 var 36 HTTP: TNetHTTPClient; 37 JO: TJSONObject; 38 Params: TStringList; 39 Response: string; 40 begin 41 Result := False; 42 43 HTTP := TNetHTTPClient.Create(nil); 44 Params := TStringList.Create(); 45 try 46 Params.Values[‘app_key‘] := AppKey; 47 Params.Values[‘format‘] := ‘json‘; 48 Params.Values[‘method‘] := ‘alibaba.aliqin.fc.sms.num.send‘; 49 Params.Values[‘sign_method‘] := ‘hmac‘; 50 Params.Values[‘timestamp‘] := FormatDateTime(‘yyyy-MM-dd HH:mm:ss‘, Now); 51 Params.Values[‘v‘] := ‘2.0‘; 52 Params.Values[‘sms_type‘] := ‘normal‘; 53 Params.Values[‘sms_free_sign_name‘] := FreeSignName; 54 Params.Values[‘rec_num‘] := ReceiveNumber; 55 Params.Values[‘sms_template_code‘] := TemplateCode; 56 Params.Values[‘sms_param‘] := TemplateContent; 57 Params.Values[‘sign‘] := MakeSign(Params, AppSecret); 58 59 HTTP.ContentType := ‘application/x-www-form-urlencoded‘; 60 try 61 Response := HTTP.Post(‘https://eco.taobao.com/router/rest‘, Params).ContentAsString(TEncoding.UTF8); 62 except 63 on E: Exception do 64 begin 65 ResultMsg := E.Message; 66 Exit; 67 end; 68 end; 69 70 JO := TJSONObject.ParseJSONValue(Response) as TJSONObject; 71 try 72 if JO <> nil then 73 begin 74 if JO.TryGetValue<string>(‘alibaba_aliqin_fc_sms_num_send_response.result.success‘, ResultMsg) then 75 Result := ResultMsg.ToUpper = ‘TRUE‘ 76 else if JO.TryGetValue<string>(‘error_response.msg‘, ResultMsg) then 77 Result := False; 78 end; 79 80 finally 81 JO.Free; 82 end; 83 84 finally 85 HTTP.Free; 86 Params.Free; 87 end; 88 89 end;
时间: 2024-10-15 04:21:16