阿里大因而阿里通訊旗下產品,融合了三大運營商的通訊能力,提供包括短信、語音、流量直充、私密專線、店鋪手機號等個性化服務。每條四分五,價錢還算公道,經老農測試,響應速度很是快,基本上是秒到。官方文檔提供了JAVA、.NET、PHP、Python、C/C++、NodeJS 等語言的 Demo,惟獨沒有 Dephi,但這也不能怪馬雲,畢竟 Delphi 實在過小衆了。算法
最近用 Delphi 寫個 App,註冊用戶須要用到手機短信驗證,因而找到的阿里大於,使用 Delphi 10.1 berlin 寫了個簡單的 Demo 並測試經過,如今交出代碼:json
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 JsonObject: 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(); 62 except 63 on E: Exception do 64 begin 65 ResultMsg := E.Message; 66 Exit; 67 end; 68 end; 69 70 JsonObject := TJSONObject.ParseJSONValue(Response) as TJSONObject; 71 try 72 if JsonObject <> nil then 73 begin 74 if JsonObject.TryGetValue<string>('alibaba_aliqin_fc_sms_num_send_response.result.success', ResultMsg) then 75 Result := ResultMsg.ToUpper = 'TRUE' 76 else if JsonObject.TryGetValue<string>('error_response.msg', ResultMsg) then 77 Result := False; 78 end; 79 80 finally 81 JsonObject.Free; 82 end; 83 84 finally 85 HTTP.Free; 86 Params.Free; 87 end; 88 89 end;
有很多同窗還在使用D7,不知道怎麼用,稍微改改就能夠了。api
1 function SendSMS(const AppKey, AppSecret, ReceiveNumber, FreeSignName, TemplateCode, TemplateContent: string; var ResultMsg: string): Boolean; 2 3 function GetStringMD5(const AInPut: string): string; 4 var 5 MD5: TIdHashMessageDigest5; 6 Digest: T4x4LongWordRecord; 7 begin 8 MD5 := TIdHashMessageDigest5.Create; 9 try 10 Digest := MD5.HashValue(AInPut); 11 Result := MD5.AsHex(Digest); 12 finally 13 MD5.Free; 14 end; 15 end; 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 Data := ''; 27 for I := 0 to AParams.Count - 1 do 28 Data := Data + StringReplace(AParams[I], '=', '', [rfReplaceAll]); 29 // MD5 算法 30 Result := GetStringMD5(AppSecret + Data + AppSecret); 31 end; 32 33 var 34 HTTP: TIdHTTP; 35 Params: TStringList; 36 Response: string; 37 JsonObject: ISuperObject; 38 begin 39 Result := False; 40 41 HTTP := TIdHTTP.Create(nil); 42 Params := TStringList.Create(); 43 try 44 Params.Values['app_key'] := AppKey; 45 Params.Values['format'] := 'json'; 46 Params.Values['method'] := 'alibaba.aliqin.fc.sms.num.send'; 47 Params.Values['sign_method'] := 'md5'; 48 Params.Values['timestamp'] := FormatDateTime('yyyy-MM-dd HH:mm:ss', Now); 49 Params.Values['v'] := '2.0'; 50 Params.Values['sms_type'] := 'normal'; 51 Params.Values['sms_free_sign_name'] := UTF8Encode(FreeSignName); 52 Params.Values['rec_num'] := ReceiveNumber; 53 Params.Values['sms_template_code'] := TemplateCode; 54 Params.Values['sms_param'] := UTF8Encode(TemplateContent); 55 Params.Values['sign'] := MakeSign(Params, AppSecret); 56 57 HTTP.HandleRedirects := True; 58 HTTP.Request.AcceptCharSet := 'utf-8'; 59 HTTP.Request.ContentType := 'application/x-www-form-urlencoded'; 60 try 61 Response := HTTP.Post('http://gw.api.taobao.com/router/rest', Params); 62 except 63 on E: Exception do 64 begin 65 ResultMsg := E.Message; 66 Exit; 67 end; 68 end; 69 70 JsonObject := SO(Response); 71 if JsonObject <> nil then 72 begin 73 ResultMsg := JsonObject.S['alibaba_aliqin_fc_sms_num_send_response.result.success']; 74 if ResultMsg <> '' then 75 Result := UpperCase(ResultMsg) = 'TRUE' 76 else 77 begin 78 ResultMsg := JsonObject.S['error_response.msg']; 79 Result := False; 80 end; 81 end; 82 83 finally 84 HTTP.Free; 85 Params.Free; 86 end; 87 88 end;