Delphi realizes WeChat development and access to WeChat public account platform

Source: Internet
Author: User
First, you need to learn the access materials. here, because the principles are all there, you must carefully read the information. Then, use Delphi to implement a corresponding function and then apply for an account for public platform interface testing. First, you need to learn the connected materials. Http://mp.weixin.qq.com/debug/cgi-bin/sandbox? T = sandbox/login

Function CheckSignature (const signature, timestamp, nonce, token: string): boolean;
Var

Strs: TStringList;
TmpStr: string;
Begin

Strs: = TStringList. Create;
Try
Strs. Add (token );
Strs. Add (timestamp );
Strs. Add (nonce );

Strs. Sort;
TmpStr: = strs [0] + strs [1] + strs [2];
TmpStr: = SHA1 (tmpStr );

If tmpStr = signature then
Result: = True
Else
Result: = False;
Finally
FreeAndNil (strs );
End;
End;
If the result returned by the function is true, the access is successful! Thank you for referring to the example uploaded by a csdn friend. In this example, the SHA1 function unit is provided, so you need to download it back.

After preparing this function, how can I use this function?
Because the server sends a Get request to the Accessed web server, we need to call this CheckSignature in our own implementation of the kbmMW web server's PerformGet method.

Function tdj_frmphonehttpservice.w.mget (ClientIdent: TkbmMWClientIdentity; const AURL: string;
Const Args: array of Variant): Variant;
Var
FuncName: string;
Qv: TkbmMWHttpQueryValues; // process the parameters passed in the Get request.
Begin

If Length (Args) <1 then
KbmMWRaiseException (KBMMW_ERR_SERVICE_HTTP_URLMISSING, 'Missing URL .')
Else
Begin
FuncName: = UpperCase (copy (Args [0], 2, Length (Args [0])-1 ));
If FuncName. Equals ('WeChat. HTML') then
Begin
// Transfer interface
Qv: = TkbmMWHttpQueryValues. Create;
Qv. AsString: = Args [2]; // Obtain the parameters passed by the get request.
Try
// Verify access
If qv. ValueByName ['echostr'] <> ''Then // if echostr is not empty, it indicates a verification request.
Begin
Result: = '';
If dmwx. CheckSignature (qv. ValueByName ['id'], qv. ValueByName ['signature'],
Qv. ValueByName ['timestamp'], qv. ValueByName ['nonce ']) then
Result: = qv. ValueByName ['echostr']; // if the verification is successful, echostr is returned, notifying the server that the verification is successful.
End;
SetResponseMimeType ('text/HTML ');
SetResponsecharset ('utf-8 ');
Finally
FreeAndnil (qv );
End;
Exit; // end the response to Get.
End;
... Skip the implementation below

In actual projects, I implement the interface code in a wechatImpl unit, and then implement a DataModule to further encapsulate the wechatImpl method, to perform database operations.

Take verification as an example to see how DataModule is implemented?

Function tdmwx. CheckSignature (id, signature, timestamp, nonce: string): Boolean;
Begin
Result: = False;
If not qWXFWH. Active then
QWXFWH. Open;
If qWXFWH. Locate ('fid', VarArrayOf ([id]), []) then // query the service number table and check whether the corresponding id record exists
Begin
If wechatImpl. CheckSignature (signature, timestamp, nonce, qWXFWH. FieldByName ('ftoken'). AsString) // exists, the token value defined in the table is used to call the verification function.
Then
Result: = true;
End;
End;
QWXFWH is an kbmMWuniDACQuery object used to save a table with service numbers. the table structure is as follows:
Create table WX_FWH (
Fid integer, // The id of a service number to manage multiple service numbers.
FTOKEN VarChar (32), // The token value of each service number
FAPPID VarChar (16 ),
FAPPSECRET varchar (16 ),
Faccesstoken varchar (512 ),
Fexpiresin integer,
Fgetdate datetime );
When registering a server, you must first add registration information in this table, for example, id = 1, token = kbmmwtoken1
The corresponding registration content is:
Url = www. test. cc/wechat.html? Id = 1
Token = kbmmwtoken1
In this way, when the server sends a verification request, the id is passed as a parameter. the CheckSignature method of datamodule obtains the token value defined in the data table based on the id value, then, call the interface verification method.

To achieve access, I have divided the implementation code into three layers:
1. call the database-layer verification function in the get method of kbmmw web server
2. implement the verification function at the database layer to obtain pre-defined number information from the table, such as id and token, and then call the specific interface.
3. implement specific interfaces for the database layer to call.

After all the implementation of interfaces such as scheduler is completed, it will take some time to share it with friends. Let's share the idea of implementation first.

Verifying the url address used to access the web server is the first step of the service. Therefore, you must understand the above content before proceeding.
This is actually the case:
Users who follow your service number send a message to your service number. the platform will forward the message to the verified url, which is equivalent to the user of your service number, send messages to your Web server. The purpose of the Web server you developed is to respond to these messages and interact with your service number owner!

For more articles about implementing development and access to the public account platform in delphi, please follow the PHP Chinese network!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.