delphi 利用阿里大于接口发短信(Delphi版)


本文整理自网络,侵删。

 
阿里大于是阿里通信旗下产品,融合了三大运营商的通信能力,提供包括短信、语音、流量直充、私密专线、店铺手机号等个性化服务。每条四分五,价钱还算公道,经老农测试,响应速度非常快,基本上是秒到。官方文档提供了JAVA、.NET、PHP、Python、C/C++、NodeJS 等语言的 Demo,唯独没有 Dephi,但这也不能怪马云,毕竟 Delphi 实在太小众了。

用到的单元整理:
uses Hash,System.Net.URLClient,
System.Net.HttpClient, System.Net.HttpClientComponent,System.json; // Dephi自带的JSON单元

  /// 全能地图(QQ:64445322)
 ///
 /// 利用阿里大于接口发短信
 /// 阿里大于网址:http://www.alidayu.com
 /// 阿里大于短信接口文档:https://api.alidayu.com/doc2/apiDetail.htm?apiId=25450
 ///
 /// TOP分配给应用的AppKey
 /// AppSecret
 /// 接收手机号码
 /// 短信签名,传入的短信签名必须是在阿里大于“管理中心-短信签名管理”中的可用签名
 /// 短信模板ID
 /// 短信模板变量,例如:{"code":"1234","product":"alidayu"}
 /// 下发结果消息
 /// 是否成功,True = 成功 ,false = 失败
 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 := 0 to AParams.Count - 1 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('alibaba_aliqin_fc_sms_num_send_response.result.success', ResultMsg) then
           Result := ResultMsg.ToUpper = 'TRUE'
         else if JO.TryGetValue('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 := 0 to AParams.Count - 1 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 webservices传数据

Delphi firedac 如何按整型(byte)读取 mysql tinyint(1) 类型字段?

Delphi ord chr byte等转换

Delphi 字符串显示后5位

Delphi 之 工具栏组件(ttoolbar)

Delphi实现背景音乐播放

Delphi firedac 下的 sqlite [10] - 使用 r-tree 搜索

Delphi fmx窗体中控件对齐方式介绍

Delphi webbrowser1 execwb 复制 新建 打开

wmi技术介绍和应用

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...