Php-based WeChat public platform development entry instance, php public platform instance _ PHP Tutorial

Source: Internet
Author: User
Tags openid php example
Php-based entry-level public platform development instance and php public platform instance. This document describes the php-based public platform development method. Share it with you for your reference. The following is an example of php-based public platform development.

This article describes the php-based public platform development method. Share it with you for your reference. The details are as follows:

I recently developed a public platform and wrote more than 20 functions in one breath, which is quite interesting ~

Let's share our development experience today ~
The interfaces provided by the public platform are simple. let's take a look at the message interaction process:

Generally speaking, you can send a message-> send data to a developer-> process a message by a developer and return data to-> send the returned data to the user, during this period, data interaction is completed through XML.

Below is an example to develop an intelligent chatbot:

1. Register a public platform account

Public platform:
Https://mp.weixin.qq.com/

Note: Currently, only two accounts can be registered for one ID card. the account name is subject to V authentication. please register with caution.

2. apply for servers/virtual hosts

BAE and SAE can be used for children's shoes without servers or virtual hosts.

3. enable the developer mode

The public platform has two modes: the editing mode (the dummies mode), which is simple but has a single function. The other is the developer mode, which can implement complex functions through development. The two modes are mutually exclusive. Obviously, log on to the public platform and enable the developer mode through the "advanced functions" menu.

4. fill in interface configuration information

You also need to configure two parameters in the "advanced functions" menu:
URL: the developer's application access address. Currently, only port 80 is supported. The example is "http://www.yoonper.com/weixin/index.php.
TOKEN: enter it as needed to generate a signature. take "YoonPer" as an example.
After entering the information, save the following code as index. php and upload it to the http://www.yoonper.com/weixin/directory. click submit to complete verification.

<? Phpdefine ("TOKEN", "YoonPer"); // TOKEN value $ wechatObj = new wechat (); $ wechatObj-> valid (); class wechat {public function valid () {$ echoStr = $ _ GET ["echostr"]; if ($ this-> checkSignature () {echo $ echoStr; exit ;}} private function checkSignature () {$ signature = $ _ GET ["signature"]; $ timestamp = $ _ GET ["timestamp"]; $ nonce = $ _ GET ["nonce"]; $ token = TOKEN; $ tmpArr = array ($ token, $ timestamp, $ nonce); sort ($ TmpArr); $ tmpStr = implode ($ tmpArr); $ tmpStr = sha1 ($ tmpStr); if ($ tmpStr ==$ signature) {return true ;} else {return false ;}}}?>

This is what the public platform verifies whether the URL is correctly accessed. The research code has no substantive significance. after the verification, you can delete the file. this is not detailed. if you are interested, you can view the official documentation.
Public platform API documentation:
Http://mp.weixin.qq.com/wiki/index.php

5. develop public platform functions

OK, as mentioned above, the data interaction between the public platform and developers is completed through XML. Since XML is used, the rules must be followed, before proceeding with the development, let's take a look at the XML specifications provided in the official interface documentation. take text messages as an example:

When a user sends a message to a public account, the server will POST some data to the developer:

 
  
  toUser
  
  
  fromUser
  
  
  
   
12345678
  
  
  
   
  
  
  content
  
  
  
   
1234567890123456
  
 

After processing the message, the developer must return the data to the server:

 
  
  toUser
  
  
  fromUser
  
  
  
   
12345678
  
  
  
   
  
  
  content
  
  
  
   
0
  
 

In addition to text messages, the public platform also allows users to send Image messages, geographic location messages, link messages, and event Pushes. Developers can also reply music messages and text messages to the public platform, for more information about message XML specifications, see the official documentation.

Let's take a look at an official PHP example. I have made some streamlining:

<? Php/* ------------------------------------------------- | index. php [public platform interface] + response | Author: limyonper + response */$ wechatObj = new wechat (); $ wechatObj-> responseMsg (); class wechat {public function responseMsg () {// ---------- receive data ---------- // $ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; // Get POST data // Parse post xml data with SimpleXML $ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA); $ fromUsername = $ postObj-> FromUserName; // Obtain the sender's account (OpenID) $ toUsername = $ postObj-> ToUserName; // Obtain the recipient's account $ keyword = trim ($ postObj-> Content ); // get the message content $ time = time (); // get the current timestamp // ---------- return data ---------- // return message template $ textTpl ="
   
  %s
    
  %s
    
  
   
% S
    
  %s
    
  %s
    
  
   
0
    
 "; $ MsgType =" text "; // message type include ('simsimi. php '); $ contentStr = simsimi ($ keyword); // return the message content // format the message template $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr); echo $ resultStr; // output result }?>

Save the code as index. php and upload it to the http://www.yoonper.com/weixin/directory. if the file is not deleted, the file will be directly overwritten.

Now users send any message through the public platform the public account returns a message with the content of "http://www.YoonPer.com.
What needs to be done next is to dynamically return results based on user messages ~

SimSimi (Xiaohuang chicken) is currently a popular chatbot. I used CURL to develop a free SimSimi (Xiaohuang chicken) interface, and a text reply will be returned when a keyword is passed in, this part is not the focus of this article, so I will not describe it much. go directly to the code (updated on January 28 ):

<? Php/* ------------------------------------------------- | simsimi. php [intelligent chat (simsimi)] + response | Author: limyonper + ------------------------------------------------ */function simsimi ($ keyword) {$ keyword = urlencode ($ keyword ); // ----------- get COOKIE ---------- // $ url =" http://www.simsimi.com/ "; $ Ch = curl_init ($ url); curl_setopt ($ ch, CURLOPT_HEADER, 1); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); $ content = curl_exec ($ ch ); list ($ header, $ body) = explode ("\ r \ n", $ content ); preg_match_all ("/set \-cookie :( [^ \ r \ n] *);/iU", $ header, $ matches); $ cookie = implode (';', $ matches [1]). "; simsimi_uid = 1;"; curl_close ($ ch); // ----------- capture and restore ---------- // $ url =" http://www.simsimi.com/ Func/reqN? Response "; $ ch = curl_init ($ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ ch, CURLOPT_COOKIE, $ cookie ); $ content = json_decode (curl_exec ($ ch), 1); curl_close ($ ch); if ($ content ['result'] = '20140901 ') {return $ content ['sentence _ resp '];} else {return' I will not answer this question yet... ';}}?>

The above two pieces of code are integrated, and the connection will be disconnected if the server fails to receive the response within five seconds. this interface may cause timeout, simSimi has blocked the crawling requests on the BAE and SAEs. we recommend that you use the official billing API of SimSimi, which is faster ~

I hope this article will help you develop php-based public platforms.

Examples in this article describe the php-based public platform development method. Share it with you for your reference. The details are as follows...

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.