Home > Backend Development > PHP Tutorial > WeChat public platform development introductory tutorial (picture and text), public introductory tutorial_PHP tutorial

WeChat public platform development introductory tutorial (picture and text), public introductory tutorial_PHP tutorial

WBOY
Release: 2016-07-12 08:52:26
Original
1255 people have browsed it

WeChat public platform development introductory tutorial (pictures and texts), public introductory tutorial

Keywords: Introduction to WeChat public platform development

Author: C Research Laboratory

Background knowledge: The development of WeChat public platform requires certain basic knowledge of PHP. PHP is an HTML embedded language and is widely used in website development. There are two data transmission methods between the WeChat server and the developer's server, namely XML and JSON. XML is mainly used to receive and send ordinary messages and event push, while user management, creating custom menus and advanced group sending require the use of JOSN format data.

In this WeChat public platform development tutorial, you can follow the tutorial to gain an overall perceptual understanding of the development framework of the WeChat public platform, which can help you get started better.

We will use the WeChat public account Xi’an Campus Maker Space as an example. See the QR code at the bottom.

This introductory tutorial will guide you through the following tasks:

                                                                                         

Apply for Sina Cloud Computing and create platform applications

Section 2

Apply for a virtual host and build an environment for the WeChat platform Here I am using the virtual space of Yunbang Internet to demonstrate for everyone. First, open the website as shown below:

                                                                       

Then register in the upper right corner according to the prompts, and select the free space of 1G.

2. After opening the space, open the control panel and you can see the host information.

 

3. Next we use 8uftp to log in to the virtual space.

 

4. After logging in, use 8uftp to upload the index.php file to the web folder in the root directory of the virtual space, as follows:

At this point, you have successfully used the virtual space to build a server for the WeChat public platform. You only need to upload the code to implement the corresponding functions through 8uftp.

Section 3: Enabling development mode and accessing the server

WeChat public platform development model

Advanced Features

WeChat public platform address: https://mp.weixin.qq.com Log in to the WeChat public platform backend, find "Basic Configuration" at the bottom of the list on the left, and click to enter

Enter the server configuration filling box.

Click the "Modify Configuration" button, and the following picture will appear.

The URL here is the domain name of the cloud application introduced in the previous article, and the Token is defined as weixin in index.php. There is no need to fill in the EncodingAESKey. Click "Randomly Generate" to automatically generate one. Select "Plaintext Mode" for the message encryption and decryption method, and then click the "Submit" button.

In the pop-up prompt box, click "OK"

After modifying the configuration as shown in the picture, click the "Enable" button

Ask "Are you sure you want to enable server configuration?", click "OK"

If it prompts "Token verification failed", you can try again several times. The WeChat server is sometimes unstable.

Note

: If you use Sina SAE, it may require real-name authentication. Please upload your ID card for real-name authentication and pass the review before trying again!

                                                         

If it still fails, please use the WeChat debugger to test whether the URL and token are correct. (Search directly on Baidu and there will be many free ones)

Section 4 Interface calling and implementation of common functions

                                                                        Example 1: API call Baidu Translation

Apply for Baidu Translation API

The following describes how to use Baidu Translate API to create a public account with translation function.

1. First, log in to apply to become a Baidu developer. The application link address is http://developer.baidu.com/. After applying to become a developer, log in to the website and select Developer Service Management from the drop-down menu of the management console, as shown in the figure below.

We can translate English into Chinese, Chinese into English, Chinese into Japanese, and Japanese into Chinese by calling Baidu Translate's API. Baidu AIP currently supports translation in these three languages
  1. First enter the Baidu Translation webpage, as shown by the arrow below
  2. After entering, click "User Guide"
  3. After entering the smart page, browse the page offline. In the middle part of the page, you can see the API interface of Baidu Translation
  4. According to the above interface, we can build the interface code:
  5. http://openapi.baidu.com/public/2.0/bmt/translate?client_id=uA6zT1kh5O1UXvTrUuFjFHiK&q={$keyword}&from=auto&to=auto

    The string "9peNkh97N6B9GGj9zBke9tGQ" is your Baidu key. You can see it by clicking on the basic information in Baidu's application center

    The key code is:
  6. case "text";

    $tranurl="http://openapi.baidu.com/public/2.0/bmt/translate?client_id=uA6zT1kh5O1UXvTrUuFjFHiK&q={$keyword}&from=auto&to=auto";//Baidu translation address

                                                                                                                                                                                           

    $transon=json_decode($transtr);//json parsing

    //print_r($transon);

    $contentStr = $transon->trans_result[0]->dst;//Read translation content

    break;

    Log in to WeChat on your mobile phone, and the final result is as follows:
  7. The code used is as follows:
<span> 1</span> <?<span>php
</span><span> 2</span> <span>//</span><span>6.2 百度翻译  C++研究室  CopyRight 2016 </span>
<span> 3</span> <span>define</span>("TOKEN", "weixin"<span>);
</span><span> 4</span> <span>$wechatObj</span> = <span>new</span><span> wechatCallbackapiTest();
</span><span> 5</span> <span>if</span> (!<span>isset</span>(<span>$_GET</span>['echostr'<span>])) {
</span><span> 6</span>     <span>$wechatObj</span>-><span>responseMsg();
</span><span> 7</span> }<span>else</span><span>{
</span><span> 8</span>     <span>$wechatObj</span>-><span>valid();
</span><span> 9</span> <span>}
</span><span>10</span> 
<span>11</span> <span>class</span><span> wechatCallbackapiTest
</span><span>12</span> <span>{
</span><span>13</span>     <span>public</span> <span>$fromUsername</span>=''<span>;
</span><span>14</span>     <span>public</span> <span>$toUsername</span>=''<span>;
</span><span>15</span>  <span>public</span> <span>function</span><span> valid()
</span><span>16</span> <span>    {
</span><span>17</span>         <span>$echoStr</span> = <span>$_GET</span>["echostr"<span>];
</span><span>18</span>         <span>if</span>(<span>$this</span>-><span>checkSignature()){
</span><span>19</span>             <span>echo</span> <span>$echoStr</span><span>;
</span><span>20</span>             <span>exit</span><span>;
</span><span>21</span> <span>        }
</span><span>22</span> <span>    }
</span><span>23</span> 
<span>24</span>     <span>private</span> <span>function</span><span> checkSignature()
</span><span>25</span> <span>    {
</span><span>26</span>         <span>$signature</span> = <span>$_GET</span>["signature"<span>];
</span><span>27</span>         <span>$timestamp</span> = <span>$_GET</span>["timestamp"<span>];
</span><span>28</span>         <span>$nonce</span> = <span>$_GET</span>["nonce"<span>];
</span><span>29</span>         <span>$token</span> =<span> TOKEN;
</span><span>30</span>         <span>$tmpArr</span> = <span>array</span>(<span>$token</span>, <span>$timestamp</span>, <span>$nonce</span><span>);
</span><span>31</span>         <span>sort</span>(<span>$tmpArr</span>,<span> SORT_STRING);
</span><span>32</span>         <span>$tmpStr</span> = <span>implode</span>(<span>$tmpArr</span><span>);
</span><span>33</span>         <span>$tmpStr</span> = <span>sha1</span>(<span>$tmpStr</span><span>);
</span><span>34</span> 
<span>35</span>         <span>if</span>(<span>$tmpStr</span> == <span>$signature</span><span>){
</span><span>36</span>             <span>return</span> <span>true</span><span>;
</span><span>37</span>         }<span>else</span><span>{
</span><span>38</span>             <span>return</span> <span>false</span><span>;
</span><span>39</span> <span>        }
</span><span>40</span> <span>    }
</span><span>41</span> 
<span>42</span>     <span>public</span> <span>function</span><span> responseMsg(){
</span><span>43</span>         <span>$postStr</span> = <span>$GLOBALS</span>["HTTP_RAW_POST_DATA"<span>];
</span><span>44</span>             <span>$postObj</span> = <span>simplexml_load_string</span>(<span>$postStr</span>, 'SimpleXMLElement',<span> LIBXML_NOCDATA);
</span><span>45</span>                 <span>$fromUsername</span> = <span>$postObj</span>-><span>FromUserName;
</span><span>46</span>                 <span>$toUsername</span> = <span>$postObj</span>-><span>ToUserName;
</span><span>47</span>                 <span>$type</span> = <span>$postObj</span>-><span>MsgType;
</span><span>48</span>                 <span>$event</span>=<span>$postObj</span>-><span>Event;
</span><span>49</span>                 <span>$Event_Key</span>=<span>$postObj</span>-><span>EventKey;
</span><span>50</span>                 <span>$mid</span>=<span>$postObj</span>-><span>MediaId;
</span><span>51</span>                 <span>$link</span>=<span>$postObj</span>-><span>Url;
</span><span>52</span>             
<span>53</span>                 <span>$latitude</span>  = <span>$postObj</span>-><span>Location_X;
</span><span>54</span>                 <span>$longitude</span> = <span>$postObj</span>-><span>Location_Y;
</span><span>55</span>                 <span>$keyword</span> = <span>trim</span>(<span>$postObj</span>-><span>Content);
</span><span>56</span>                 <span>$time</span> = <span>time</span><span>();
</span><span>57</span>                  <span>$textTpl</span> = "<span><xml>
</span><span>58</span> <span>                            <ToUserName><![CDATA[%s]]></ToUserName>
</span><span>59</span> <span>                            <FromUserName><![CDATA[%s]]></FromUserName>
</span><span>60</span> <span>                            <CreateTime>%s</CreateTime>
</span><span>61</span> <span>                            <MsgType><![CDATA[text]]></MsgType>
</span><span>62</span> <span>                            <Content><![CDATA[%s]]></Content>
</span><span>63</span>                             </xml>"<span>; 
</span><span>64</span>                             <span>if</span>(<span>$keyword</span>!=''<span>){
</span><span>65</span>                             <span>$id</span>="R90FXoW4OPtCbLkD9Aiaihz0"<span>;
</span><span>66</span> <span>$url</span>="http://openapi.baidu.com/public/2.0/bmt/translate?client_id=<span>$id</span>&q=<span>$keyword</span>&from=auto&to=auto"<span>;
</span><span>67</span> <span>$res</span>=<span>file_get_contents</span>(<span>$url</span><span>);
</span><span>68</span> <span>$res</span>=json_decode(<span>$res</span>,<span>true</span><span>);
</span><span>69</span> <span>$contentStr</span>=<span>$res</span>['trans_result'][0]['dst'<span>];
</span><span>70</span> }                    <span>$resultStr</span> = <span>sprintf</span>(<span>$textTpl</span>, <span>$fromUsername</span>, <span>$toUsername</span>, <span>$time</span>, <span>$contentStr</span><span>);
</span><span>71</span>                     <span>echo</span> <span>$resultStr</span><span>;
</span><span>72</span> <span>  }
</span><span>73</span> <span>}
</span><span>74</span> ?>
Copy after login

                                                                     

Example 2: API call little yellow chicken

1. Register a simsimi account

URL: http://developer.simsimi.com/signUp

2. Activate account

3. Obtain API Key

4. Specific implementation

Call the Little Yellow Chicken API to implement

Call the simsim($keyword) function and replace "Your API Key" with the applied API Key.

At the same time, the WeChat public account can also realize weather query, train query, express query, membership card, coupons, carousel, micro website, 3G photo album and so on. Micro menu, micro website, micro membership, micro group purchase, micro survey, micro photo album, micro push, micro statistics, micro payment, micro customer service, and other functions.

The above is a simple basic tutorial on WeChat public platform. If you understand it with your heart and feel that you have gained something, everything starts with practice. Please follow the tutorial to start your WeChat development journey. Bar! ! If there is anything you still don't understand, please scan the QR code of the official account to follow, reply "code", "WeChat tutorial"... All video tutorials and code information are waiting for you! ! !

Thank you for reading, please understand with your heart! Hope this helps you as a beginner! ! Sharing is also a joy! ! ! Please pass on. . .

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1126417.htmlTechArticleIntroduction to WeChat public platform development tutorial (picture and text), public introductory tutorial keywords: Introduction to WeChat public platform development author: Background knowledge of Laboratory C: The development needs of WeChat public platform...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template