利用阿里大于接口发短信(Delphi版)
阿里大于是阿里通信旗下产品,融合了三大运营商的通信能力,提供包括短信、语音、流量直充、私密专线、店铺手机号等个性化服务。每条四分五,价钱还算公道,经老农测试,响应速度非常快,基本上是秒到。官方文档提供了JAVA、.NET、PHP、Python、C/C++、NodeJS 等语言的 Demo,唯独没有 Dephi,但这也不能怪马云,毕竟 Delphi 实在太小众了。
最近用 Delphi 写个 App,注册用户需要用到手机短信验证,于是找到的阿里大于,使用 Delphi 10.1 berlin 写了个简单的 Demo 并测试通过,现在交出代码:
/// <author>全能地图(QQ:64445322)</author>
/// <summary>
/// 利用阿里大于接口发短信
/// 阿里大于网址:http://www.alidayu.com
/// 阿里大于短信接口文档:https://api.alidayu.com/doc2/apiDetail.htm?apiId=25450
/// </summary>
/// <param name="AppKey">TOP分配给应用的AppKey</param>
/// <param name="AppSecret">AppSecret</param>
/// <param name="ReceiveNumber">接收手机号码</param>
/// <param name="FreeSignName">短信签名,传入的短信签名必须是在阿里大于“管理中心-短信签名管理”中的可用签名</param>
/// <param name="TemplateCode">短信模板ID</param>
/// <param name="TemplateContent">短信模板变量,例如:{"code":"1234","product":"alidayu"}</param>
/// <param name="ResultMsg">下发结果消息</param>
/// <returns>是否成功,True = 成功 ,false = 失败</returns>
function SendSMS(const AppKey, AppSecret, ReceiveNumber, FreeSignName, TemplateCode, TemplateContent: string; var ResultMsg: string): Boolean; // 签名算法:http://open.taobao.com/doc2/detail.htm?articleId=101617&docType=1&treeId=1
function MakeSign(const AParams: TStringList; const AppSecret: string): string;
var
I: Integer;
Data: string;
begin
// 参数排序
AParams.Sort; // 参数拼接
Data := '';
for I := to AParams.Count - do
Data := Data + AParams[I].Replace('=', ''); // HMAC 算法
Result := THashMD5.GetHMAC(Data, AppSecret).ToUpper;
end; var
HTTP: TNetHTTPClient;
JO: TJSONObject;
Params: TStringList;
Response: string;
begin
Result := False; HTTP := TNetHTTPClient.Create(nil);
Params := TStringList.Create();
try
Params.Values['app_key'] := AppKey;
Params.Values['format'] := 'json';
Params.Values['method'] := 'alibaba.aliqin.fc.sms.num.send';
Params.Values['sign_method'] := 'hmac';
Params.Values['timestamp'] := FormatDateTime('yyyy-MM-dd HH:mm:ss', Now);
Params.Values['v'] := '2.0';
Params.Values['sms_type'] := 'normal';
Params.Values['sms_free_sign_name'] := FreeSignName;
Params.Values['rec_num'] := ReceiveNumber;
Params.Values['sms_template_code'] := TemplateCode;
Params.Values['sms_param'] := TemplateContent;
Params.Values['sign'] := MakeSign(Params, AppSecret); HTTP.ContentType := 'application/x-www-form-urlencoded';
try
Response := HTTP.Post('https://eco.taobao.com/router/rest', Params).ContentAsString();
except
on E: Exception do
begin
ResultMsg := E.Message;
Exit;
end;
end; JO := TJSONObject.ParseJSONValue(Response) as TJSONObject;
try
if JO <> nil then
begin
if JO.TryGetValue<string>('alibaba_aliqin_fc_sms_num_send_response.result.success', ResultMsg) then
Result := ResultMsg.ToUpper = 'TRUE'
else if JO.TryGetValue<string>('error_response.msg', ResultMsg) then
Result := False;
end; finally
JO.Free;
end; finally
HTTP.Free;
Params.Free;
end; end;
D7的版本
function SendSMS(const AppKey, AppSecret, ReceiveNumber, FreeSignName, TemplateCode, TemplateContent: string; var ResultMsg: string): Boolean; function GetStringMD5(const AInPut: string): string;
var
MD5: TIdHashMessageDigest5;
Digest: T4x4LongWordRecord;
begin
MD5 := TIdHashMessageDigest5.Create;
try
Digest := MD5.HashValue(AInPut);
Result := MD5.AsHex(Digest);
finally
MD5.Free;
end;
end; // 签名算法:http://open.taobao.com/doc2/detail.htm?articleId=101617&docType=1&treeId=1
function MakeSign(const AParams: TStringList; const AppSecret: string): string;
var
I: Integer;
Data: string;
begin
// 参数排序
AParams.Sort;
// 参数拼接
Data := '';
for I := to AParams.Count - do
Data := Data + StringReplace(AParams[I], '=', '', [rfReplaceAll]);
// MD5 算法
Result := GetStringMD5(AppSecret + Data + AppSecret);
end; var
HTTP: TIdHTTP;
Params: TStringList;
Response: string;
JsonObject: ISuperObject;
begin
Result := False; HTTP := TIdHTTP.Create(nil);
Params := TStringList.Create();
try
Params.Values['app_key'] := AppKey;
Params.Values['format'] := 'json';
Params.Values['method'] := 'alibaba.aliqin.fc.sms.num.send';
Params.Values['sign_method'] := 'md5';
Params.Values['timestamp'] := FormatDateTime('yyyy-MM-dd HH:mm:ss', Now);
Params.Values['v'] := '2.0';
Params.Values['sms_type'] := 'normal';
Params.Values['sms_free_sign_name'] := UTF8Encode(FreeSignName);
Params.Values['rec_num'] := ReceiveNumber;
Params.Values['sms_template_code'] := TemplateCode;
Params.Values['sms_param'] := UTF8Encode(TemplateContent);
Params.Values['sign'] := MakeSign(Params, AppSecret); HTTP.HandleRedirects := True;
HTTP.Request.AcceptCharSet := 'utf-8';
HTTP.Request.ContentType := 'application/x-www-form-urlencoded';
try
Response := HTTP.Post('http://gw.api.taobao.com/router/rest', Params);
except
on E: Exception do
begin
ResultMsg := E.Message;
Exit;
end;
end; JsonObject := SO(Response);
if JsonObject <> nil then
begin
ResultMsg := JsonObject.S['alibaba_aliqin_fc_sms_num_send_response.result.success'];
if ResultMsg <> '' then
Result := UpperCase(ResultMsg) = 'TRUE'
else
begin
ResultMsg := JsonObject.S['error_response.msg'];
Result := False;
end;
end; finally
HTTP.Free;
Params.Free;
end; end;
利用阿里大于接口发短信(Delphi版)的更多相关文章
- 利用阿里大于实现发送短信(JAVA版)
本文是我自己的亲身实践得来,喜欢的朋 友别忘了点个赞哦! 最近整理了一下利用阿里大于短信平台来实现发送短信功能. 闲话不多说,直接开始吧. 首先,要明白利用大于发送短信这件事是由两部分组成: 一.在阿 ...
- Laravel5中使用阿里大于(鱼)发送短信验证码
在做用户注册和个人中心的安全管理时,我实现借助第三方短信平台(阿里大于(鱼))在Laravel框架中进行手机验证的设置:阿里大于,是阿里通信旗下优质便捷的云通信服务平台,整合了三大运营商的通信能力,为 ...
- 一百一十六:CMS系统之使用阿里大于sdk发送短信验证码
阿里大于短信平台:https://dysms.console.aliyun.com/dysms.htm#/overview 使用教程:https://blog.csdn.net/qq103189393 ...
- 用Python调用华为云API接口发短信
[摘要] 用Python调用华为云API接口实现发短信,当然能给调用发短信接口前提条件是通过企业实名认证,而且有一个通过审核的短信签名,话不多说,showcode #!/usr/bin/python3 ...
- 基于ThinkPHP与阿里大于的PHP短信验证功能
https://blog.csdn.net/s371795639/article/details/53381274 PHP阿里大鱼短信验证 第一步 登陆阿里大于注册账号,在用户管理中心创建应用,确定A ...
- 使用阿里大于平台发送短信验证码java代码实现
待续..网站app后台还未完成,不能添加签名,短信不能正常发送. Tip: https://help.aliyun.com/document_detail/55284.html?spm=5176.sm ...
- android 中调用接口发送短信
android中可以通过两种方式发送短信 第一:调用系统短信接口直接发送短信:主要代码如下: //直接调用短信接口发短信 SmsManager smsManager = SmsManager.getD ...
- 谁说程序员不懂浪漫?用Python每天自动给女朋友免费发短信
前言 之前发过一篇文章,用 Python 制作的给父母天气预报提醒的小工具天气变冷了,给父母制作一个天气提醒小助手,这篇文章我同步到博客上之后,有读者在评论区留言,对于部分微信没有网页版接口,导致无法 ...
- PHP阿里大于发短信教程
PHP阿里大于发短信教程 1 先去控制台 https://www.alidayu.com/center/user/account?spm=a3142.7791109.1999204004.5.ZtBQ ...
随机推荐
- Netty 源码 ChannelHandler(四)编解码技术
Netty 源码 ChannelHandler(四)编解码技术 Netty 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) 一.拆包与粘 ...
- c++11 stl 学习之 shared_ptr
shared_ptr智能指针 shared_ptr 的声明初始化方式由于指针指针使用explicit参数 必须显示声明初始化shared_ptr<string> pNico = new s ...
- openssl编译安装-各种蛋疼
arm平台 ubuntu Linux xxxxxx 3.0.62 #2 PREEMPT Tue Apr 2 20:14:12 CST 2013 armv7l armv7l armv7l GNU/Li ...
- UD系统主定制界面
- Redis的基操
redis:通常BOLEAN操作类型,操作成功返回1,操作失败返回0 通常如果往设置的key插入值,但是这个key不存在,redis则会创建 向redis里的某个key插入多个值时,值和值之间用空格隔 ...
- spring学习九 spring aop详解
本文来自于:https://www.cnblogs.com/jingzhishen/p/4980551.html AOP(Aspect-Oriented Programming,面向方面编程),可以说 ...
- Desktop Central帮助您升级Windows 10,获取更新的五大增强功能
- day13作业—(登录注册)
2.写一个函数完成三次登陆功能: 用户的用户名密码从一个文件register中取出. register文件包含多个用户名,密码,用户名密码通过|隔开,每个人的用户名密码占用文件中一行. 完成三次验证, ...
- python对数据类型的相关操作
一.int的相关操作 int只有一个相关操作,bit_length() 用于计算一个数字的二进制长度 二.bool的相关操作 1.把数字转换成bool,除了0,返回的都是True a = 10 p ...
- java.security.cert.CertificateException: No subject alternative names matching IP address xxx.xxx.xxx.xxx found
https与http不同的是,https加密,需要验证证书,而http不需要. 在连接的代码中加上: static { disableSslVerification(); } private stat ...