Getting started with WeChat public platform development and getting started with the public

Source: Internet
Author: User

Getting started with public platform development (image and text) and getting started with the public

 

Keywords:Getting started with public platform development

Author: C ++ Research Center

 

Background: The development of public platforms requires some basic PHP knowledge. PHP is an embedded HTML language and is widely used in website development. The server and the developer's server have two data transmission modes: XML and JSON. XML is mainly used to receive and send common messages and event pushes, while JOSN format data is required for user management, custom menu creation, and advanced group sending.

 

In this public platform development tutorial, you can follow the tutorial to learn about the development framework of the public platform as a whole and help you better start with it.

We will use the public account Xi'an campus maker space as an example. The QR code can be found at the bottom.

This tutorial will guide you through the following tasks:

 

Section 1

 

Apply for Sina cloud computing and create platform applications

 

 

 

Section 2Apply for a VM and set up an environment for the Platform

Here I use the virtual space of Cloud State interconnection for demonstration. First, open the website, such:

 

Then, follow the prompts in the upper right corner to register and select 1 GB free space.

2. Open the Control Panel after activating the space to view the host information.

3. Next we will use8 uftpLog on to the virtual space.

 

 

4. Use8 uftpUpload the index. php file to the web folder in the root directory of the virtual space, as shown below:

Now, you can use a virtual space to build a server for the public platform. In the future, you only need to upload the code that implements the corresponding functions through 8uftp.

 

 

 

Section 3 enable the development mode and Access Server

Public platform development model

Advanced functions

Public platform address: https://mp.weixin.qq.com

Log on to the background of the public platform, find "basic configuration" at the bottom of the list on the left, and click to enter

Enter the server configuration field.

Click "modify configuration.

The URL here is the domain name of the cloud application described in the previous article, and the Token is defined as weixin in index. php. EncodingAESKey is not required. Click "randomly generated" to generate one automatically. Select "plaintext mode" for message encryption and decryption, and click "Submit.

In the pop-up box, click "OK"

After the configuration is modified, click "enable ".

Ask "are you sure you want to enable server configuration" and click "OK"

If the message "failed token Verification" is displayed, you can repeat it several times and the server is sometimes unstable.Note: If you use Sina SAE, you may require real-name authentication. please upload your ID card for real-name authentication and pass the review. Then try again!

 

 

 

 

If it still fails, use the debugger to check whether the url and token are correct. (Directly inBaidu searchThere will be a lot of free)

Section 4Interface call and implementation of common functions

Example 1: API call-Baidu Translation

Apply for Baidu translation API

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

1, first of all, login application to become Baidu developers, the application link address for http://developer.baidu.com /. After applying to become a developer, log on to the website and select developer service management from the drop-down menu on the Management Console, as shown in.

  1. By calling Baidu translation API, we can translate English into Chinese, Chinese into English, Chinese into Japanese, and Japanese into Chinese. Baidu AIP currently supports translation in these three languages

  2. First, go to the Baidu translation webpage. The website is shown in the Arrow below.

  3. Click "User Guide"

  4. Go to the smart page and browse the page offline. In the middle of the page, you can see the API interface of Baidu translation.

  5. Based on the above interface, we can build the interface code:

    Http://openapi.baidu.com/public/2.0/bmt/translate? Client_id = uA6zT1kh5O1UXvTrUuFjFHiK & q ={$ keyword} & from = auto & to = auto

    "9peNkh97N6B9GGj9zBke9tGQ" is your Baidu key. Click basic information in the Baidu Application Center.

  6. Key code:

    Case "text ";

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

    $ Transtr = file_get_contents ($ tranurl); // read the file

    $ Transon = json_decode ($ transtr); // json Parsing

    // Print_r ($ transon );

    $ ContentStr = $ transon-> trans_result [0]-> dst; // read translation content

    Break;

  7. Log on to your mobile phone. The final result is as follows:

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

 

 

Example 2: Little Yellow Chicken

1. Register a simsimi account

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

2. Activate an account

3. Obtain the API Key

4. Specific implementation

Call the yellow rooster API for implementation

Call the simsim ($ keyword) function for processing. Replace "Your API Key" with the applied API Key.

// CopyRight 2016 public function simsim ($ keyword) {$ key = "41250a68-3cb5-43c8-9aa2-d7b3caf519b1"; $ url_simsimi = "http://sandbox.api.simsimi.com/request.p? Key = ". $ key. "& lc = ch & ft = 0.0 & text = ". $ keyword; $ json = file_get_contents ($ url_simsimi); // read the entire file into a string $ result = json_decode ($ json, true ); // encode a JSON string // $ errorCode = $ result ['result']; // use $ response = $ result ['response'] for debugging. // if (! Empty ($ response) {return $ response;} else {$ ran = rand (); switch ($ ran) {case 1: return "the chicken is tired today, chat with you tomorrow. "; Break; case 2: return" go to bed ~~ "; Break; case 3: return" Call ~~ Call ~~ "; Break; case 4: return" you have a lot to talk about, don't talk to you "; break; case 5: return" thank you for choosing Xi'an campus maker space ". "\ n ". "No.: xaxymaker ". "\ n"; break; default: return "thank you for choosing Xi'an campus maker space ". "\ n ". "No.: xaxymaker ". "\ n"; break ;}}}

 

 

At the same time, the public account can also be used to query the weather by train, and query the membership card, coupons, big turntable, micro-website, and 3G album. Micro menu, micro website, micro Member, micro group buying, Micro Research, micro album, micro push, micro statistics, Micro Payment, micro customer service, and so on.

 

The above is a simple basic tutorial on the public platform. If you feel that you have gained some benefits, everything begins with practice. Please start with the tutorial and start your development journey !! If you do not understand anything, scan the QR code of the Public Account and Reply to "code", "tutorial"... all the video tutorial code materials are waiting for you !!!

Thank you for reading this article! Hope you can help beginners !! Sharing is also a pleasure !!! Please relay...

 

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.