利用阿里大于接口发短信(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 ...
随机推荐
- c++文件中引用C代码
下面提供一个比较完整的示例程序,一共有四个文件:main.cpp.test.c.test.h.test.hpp main.cpp #include "test.hpp" int m ...
- sqli-labs:17,增删改
增 insert into users values(','lcamry','lcamry'); 删 delete from users where id=16 删数据库:drop database ...
- PHP--根据手机号-淘宝平台获取归属地运营商信息
//获取手机账号信息 public function get_mobile_area($mobile){ $sms = array('province'=>'', 'supplier'=> ...
- DB2 OLAP函数的使用
说起 DB2 在线分析处理,可以用很好很强大来形容.这项功能特别适用于各种统计查询,这些查询用通常的SQL很难实现,或者根本就无发实现.首先,我们从一个简单的例子开始,来一步一步揭开它神秘的面纱,请看 ...
- 【Web】Nginx配置开机启动
在添加nginx服务之后,大家会希望开机伴随启动nginx,避免手动路径输入启动: nginx官方提供了启动脚本:https://www.nginx.com/resources/wiki/start/ ...
- 860. Lemonade Change
class Solution { public: bool lemonadeChange(vector<int>& bills) { , ten = ; for (int i : ...
- PHP实现图片批量压缩
set_time_limit(0); global $source_dir; global $target_dir; $source_dir = "D:/images/";//目标 ...
- chmod用法
以下是chmod的详细用法:chmod命令用于改变linux系统文件或目录的访问权限.用它控制文件或目录的访问权限.该命令有两种用法.一种是包含字母和操作符表达式的文字设定法:另一种是包含数字的数字设 ...
- 重启随机游走算法(RWR:Random Walk with Restart)
1 pagerank算法的基本原理 Pagerank算法是Google的网页排名算法,由拉里佩奇发明.其基本思想是民主表决.在互联网上,如果一个网页被很多其他网页所链接,说明它受到普遍的承认和信赖,那 ...
- Forward团队-爬虫豆瓣top250项目-代码设计规范
组长地址:http://www.cnblogs.com/mazhuangmz/p/7603641.html 成员:马壮,李志宇,刘子轩,年光宇,邢云淇,张良 1.缩进采用一个Tab键 2.大括号:如: ...