WeChat public platform development Getting Started Tutorial, public Getting Started Tutorial _ PHP Tutorial

Source: Internet
Author: User
Getting started with public platform development (text) and getting started with the public. Getting started with public platform development (text), getting started with public tutorials Keywords: Getting started with public platform development author: C ++ research center background knowledge: public platform development requires the public platform development Getting Started Tutorial (text), public Getting Started Tutorial

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
 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 ($ postStr, '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 ="
 
  
58
  %s
  59
  %s
  60
  
   
% S
  61
  text
  62
  %s
  63
 "; 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...

Author: C ++ research center background: development needs of the public platform...

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.