Lecture 2 exploring WeChat Public Accounts

Source: Internet
Author: User
Tags sha1 sha1 encryption

This series of articles belongs to the author's original articles. Please respect the author's Labor achievements. For reprinted articles, please indicate the source: walkingmanc's column. If you think this series of articles is useful to you, please recommend it to more people to benefit more people. Thank you. At the same time, some friends have reported that they want to communicate with each other. We have created a group for your convenience. Welcome to join the QQ group: 364072602 first of all, 2nd articles were delayed because we had worked too much overtime and it was difficult to coordinate the time. Sorry. I will stay up late every day to write this series.
 
In the previous article, we explained how to apply for a public account, the basic principles of public platform development, and how to set up the server environment. At the beginning of this lecture, we officially started the development of the public account. 1. Interface

As we mentioned in the previous article, when the platform (whether the subscription number or service number) interacts with our website, it is implemented through the interface calls that the platform opens to external systems. That is to say, after a fan sends an interaction (text, image, sound, or menu click) to the public account, the platform sends a call request to the website corresponding to the url you configured, to execute the code of your website, and then return a processing result after you perform various processing in the code.

Your website's specific language for development depends on your hobbies. However, I suggest you use php, because php is the most convenient for web development, of course, you can also write in java. It doesn't matter. Let's take php as an example.

When configuring a url, the platform verifies whether the website of your corresponding url exists and whether it is your own website. The verification principle is very simple. You should first enter a Token (you don't have to worry about what this is, just remember that it is actually a string symbol used to generate a signature representing your own identity ), enter the url of your website and click submit, as shown in.


The Platform sends a GET request to the website corresponding to the url, carrying four parameters at the same time. For more information about the parameters, see Figure 2. For example.



Timestamp is a timestamp. The timestamp is the total number of seconds from January 1, January 1, 1970 to the current time. You just need to count. How many seconds have elapsed since the time of June January 1, 1970 and now? What is the timestamp. Therefore, the timestamp is different at any time. Nonce and echostr are random numbers generated by the platform. Signature is the Signature, which is calculated as follows:

Sort the current timestamp, nonce, and TOKEN in alphabetical order to form a string. For example, if the timestamp is 345632, the nonce is 132a, And the TOKEN is 123, we first sort them in alphabetical order to obtain the order 123,132 a, 345632. then splice the data to 123132a345632. finally, the Signature is obtained through SHA1 encryption: E69E5E13D8B045923EF1BE38DF938F5621CA3004. SHA1 is an encryption algorithm. We will not discuss the details here. These parameters will be sent to our website in the form of query parameters.

To accept these parameters, we need a php website. OK. Now we will start building such a website.

1. download the WAMP integrated installation kit. This is very simple. You can click "Next" and click "finish". I installed it directly in the C root directory, the directory structure after installation is shown in the figure below. after the installation is complete, we can see that the required apache, mysql, and php have been installed, and a management tool is installed for you by default, which is very convenient. The most important thing is the www directory, which is the root directory of the website we are about to create.



2. Enter the www directory, create a new index. php file, and write only one line of code in it, as shown in.




Enter localhost in the browser. If the page appears, it indicates that our server environment is OK, and we can proceed to the next step.

3. Next, let's start code processing. First, we obtain the four parameters passed by the platform,

 

For specific code, see the following, which has been described in detail. Let's talk about why php is used. We don't see that there are only less than 100 lines of php code, but I used java to implement the same function, but I spent more than 500 lines, this benefits from many php functions and helps us to use them directly. Therefore, php is especially suitable for development of small and medium-sized websites. You can try another method in other languages.






Here, I will explain the code above: the code processing process is like this. The timestamp, nonce, and TOKEN you input will be passed in, so of course you know how many tokens are) sort, splice, and SHA1, and finally get a signature.

 

Because the Signature process we get is exactly the same as that processed by the platform, the Signature we get and the Signature value sent to us by the platform should be exactly the same.

Compare the obtained result with the Signature of the passed Signature. If it is equal, return echostr (echostr is also a parameter sent to us by the platform) to the platform, tell the platform that I have received your request, and this request has not been tampered with during network transmission (because the signature is equal ). In this way, the verification is passed, which is equivalent to establishing a trusted communication channel between the platform and our website.

If the following information is displayed on the platform, it indicates that the verification is successful and you can start your development.





. 2. Use php to develop the server

Our mall is actually a web program, but the web program client is no longer a browser but a platform, so, any language that supports web server development can be used as the development language of our mall, such as Java and php. Why should I select php here, because php is the most suitable language for web development so far, and with our in-depth development, you will find that php is powerful in web development, many java functions can be implemented only by a lot of lines of code. php can be implemented with only one sentence of code. Therefore, we chose php as our sample development language.

So, when you use php for development, I believe that when debugging is mentioned, many people will still feel a headache, especially new users. I have seen many people complain that php debugging is very inconvenient. It is really troublesome to use echo, print, and other output statements to output variable values for debugging, these output statements cannot set breakpoints. Therefore, I am here to help you solve the php breakpoint debugging problem first, because there is no "powerful tool" for breakpoint debugging, and it will be very painful for you to solve problems in future development.

Speaking of breakpoint debugging in php, I believe that many of you first think of zend studio + zend debugger. Yes, you can do this, but doing so means you need to manually configure many things, in addition, they can stay at the breakpoint. Here, I will not spare you any more. I will teach you a very convenient tool and install it directly to debug php.

This tool is a combination of XAMPP and PhpEclipse. XAMPP is an integrated installation package, which includes Apache, MySQL, PHP, and Perl. In XAMPP, A represents Apache, M represents MySQL, the first P represents PHP, and 2nd P represents Perl. this tool carries the php debugging function xdbug. After installation, You can debug php without any special configuration.

 

 

Follow the steps below:

1. Open http://www.apachefriends.org/zh_cn/download.html to download XAMPP for Windows1.8.2 & 1.8.3


2. decompress the package and double-click the xampp-win32-1.8.0-VC9-installer.exe

3. Click "Next". When selecting the file installation location, install the file in the C: \ disk root directory.




4. Waiting for Installation



5. Complete.



6. The control panel is automatically opened. One is the button for starting various services, the other is the button for configuring various services, and the other is the button for viewing logs of various services.

7. Click the Start button of Apache and MySql to Start the apache web server and mysql database server. If a value is displayed below the Pid and ports, the service is successfully started.

If an error occurs during startup, check whether the port is occupied. Modify the port configuration in the configuration file and restart.




Download and decompress phpeclipse and click Run. You will find that the smart phpeclipse has automatically discovered xampp and configured it for you.




Next, we will create a php website project and verify whether our xampp can debug our php website. Only when our debugging works properly can I help you analyze the details of the interaction process between the platform and our Website step by step in future courses, so that you can truly understand the principles, then you can use debugging to analyze and solve problems in practice.

 

1. First, create a php project. Add a PHP file and add some simple code. And set several breakpoints. Note: When you set a BreakPoint, do not double-click it. You must right-click it on the left and choose Toggle Xdebug BreakPoint from the context menu. This is because double-click sets the java breakpoint, and we need to set the breakpoint for php, which must be set by right-clicking. In this way, the php breakpoint is set, this breakpoint XDebug can be identified. Otherwise, you may easily encounter a problem where you cannot stop at the breakpoint.







2. Next, open the installation directory of our xampp, Which is C: \ xampp \ php, and open the php. ini configuration file in notepad. We need to open the comment before the last part of the content about the xdebug configuration. The modified values are as follows:







In this way, the xdebug debugging function of xampp is enabled. If you are not sure whether the debugging function has been enabled, you can create any php project by yourself, write a file in it, and write phpinfo () in the file (); run the following command to check whether the following output exists:





3. In this way, we have completed the server configuration. Next we need to set up in phpeclipse so that our ide can communicate with xdebug on our server. Come with me. Open phpeclipse under windows-prefrences and check whether the configuration is correct as shown in.




4. Next we can test whether our debugging function can be properly debugged. Together. Right-click file. php and choose debug as> debugconfiguration.




5. After the configuration is saved, you only need to select the configuration We Just configured under the debug icon on the toolbar for debugging later, as shown in.







6. In this case, enter http: // localhost: 8080/phpTest/file. php In the browser that comes with phpeclipse or in the IE browser? XDEBUG_SESSION_START = testID
Note the following parameter: XDEBUG_SESSION_START = testID. The testID here is the ID string we set in the debug configuration ..




7. Finally, the effect of stopping a breakpoint is shown in.





With the breakpoint single-step debugging function, it is much easier for us to do whatever php website we do next. In the next lecture, I will use php single-step debugging to give you an in-depth analysis of the principles of mall development.


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.