PHP-based WeChat public platform development example, PHP Public platform Example _php tutorial

Source: Internet
Author: User
Tags format message openid php example

PHP-based public platform development example, PHP public platform example


This paper describes the development method of public platform based on PHP. Share to everyone for your reference. Specific as follows:

Recently in the development of public platforms, a breath of writing more than 20 functions, very interesting ~

Today to share the development experience ~
The interface provided by the public platform is simple, first look at the message interaction process:

In a more popular way, the user sends the data to the developer----a developer who processes the message and returns data to the user using the Send message, which is as simple as when the data interaction is done through XML.

Let's write an example to develop an intelligent chat robot:

1. Register for Public platform account

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

Note: At present, an ID card can only register two accounts, account name is related to the certification of V, please register carefully.

2. Request a server/virtual host

Children's shoes without a server/virtual host can be used by BAE and SAE, without much introduction.

3. Open Developer mode

The public platform has two modes, one is the edit mode (fool mode), simple but functional single. Another is the developer mode, which can be developed to implement complex functions. Two modes of mutual exclusion, it is obvious that login to the public platform and the "Advanced Features" menu to open developer mode.

4. Fill in the interface configuration information

Also configured in the Advanced Features menu, you need to configure two parameters:
URL: Developer app Access address, currently only supports 80 ports, take "http://www.YoonPer.com/weixin/index.php" as an example.
TOKEN: Feel free to fill in, use to generate signature, take "Yoonper" as an example.
Fill out the following code to save as index.php and upload to the http://www.YoonPer.com/weixin/directory, and finally click "Submit" to complete the 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 thing is public platform check URL is correct access, research code is not meaningful, the verification can delete files, it is not explained in detail, interested children shoes can view official documents.
Public Platform API Documentation:
http://mp.weixin.qq.com/wiki/index.php

5. Development of public platform functions

OK, as mentioned above, the data interaction between the public platform and the developer is done through XML, and since XML is used, of course, the specification is followed, so look at the XML specification provided by the official interface documentation before embarking on the development, taking text messages as an example:

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

 
  
  
   
  
   toUser
  
   
  
   
  
   fromUser
  
   
  
   
  
   
   
  
   
  12345678 
   
  
   
   
    
  
   
  
   
  
    content
  
   
  
   
  
   
    
   1234567890123456
  
   
 
  

Developers need to return data to the server after processing the message:

 
  
  
   
  
   toUser
  
   
  
   
  
   fromUser
  
   
  
   
  
   
   
  
   
  12345678 
   
  
   
   
    
  
   
  
   
  
    content
  
   
  
   
  
   
    
   0
  
   
 
  

In addition to text messages, the public platform also enables users to send picture messages, geo-location messages, link messages, event pushes, and developers can reply to the public platform for music messages and text messages, and the various message XML specifications can also be found in the official documentation.

To take a look at the official PHP example, I did some streamlining:

<?php/*-------------------------------------------------|   index.php [Public platform interface]+--------------------------------------------------| author:limyoonper+------------------------------------------------*/$wechatObj = new WeChat (); $WECHATOBJ Responsemsg (), class WeChat {public Function responsemsg () {///----------Receive data----------//$postStr = $GLOBALS ["HTT P_raw_post_data "]; Get post Data//Parse post with simplexml xml data $POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', libxml_nocdata)  ; $fromUsername = $POSTOBJ->fromusername; Get Sender Account (OpenID) $toUsername = $POSTOBJ->tousername; Get the receiver account $keyword = Trim ($postObj->content); Gets the message content $time = time (); Gets the current timestamp//----------return Data----------////return message template $TEXTTPL = "
 
   
    
    %s 0 
    %s 
 
    
    %s 
 
    
    %s 
 
    
    %s 
 
      
   
   
   
   
   
   

  
 "; $msgType = "text";  Message type include (' simsimi.php '); $CONTENTSTR = Simsimi ($keyword);  Return message content//format 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, or overwrite it directly if you haven't deleted the file.

Now the user sends any message through the public platform the public account will return a message with the content "http://www.YoonPer.com".
The next thing to do is to dynamically return results based on user messages ~

Simsimi (small yellow chicken) is the current comparison of fire chat robot, I used Curl developed a free simsimi (small yellow chicken) interface, the incoming keyword will return text reply, this part is not the focus of this article, not many instructions, directly on the code (2014.07.28 update):

<?php/*-------------------------------------------------|   simsimi.php [Smart Chat (Simsimi)]+--------------------------------------------------| author:limyoonper+------------------------------------------------*/function Simsimi ($keyword) {$keyword =  UrlEncode ($keyword);  -----------obtain a 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\r\n", $content);  Preg_match_all ("/set\-cookie: ([^\r\n]*);/iu", $header, $matches); $cookie = implode ('; ', $matches [1]). ";  simsimi_uid=1; ";  Curl_close ($ch); -----------Crawl reply----------//$url = "http://www.simsimi.com/func/reqN?lc=ch&ft=0.0&req= $keyword &fl=  Http%3a%2f%2fwww.simsimi.com%2ftalk.htm ";  $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 '] = = ' + ') {return $content [' Sentence_resp '];  } else {return ' I will not answer this question ... '; }}?>

The above two pieces of code together is done, it is necessary to note that the server in 5 seconds to receive the response will be disconnected, through this interface may time out, and Simsimi has blocked the BAE and SAE crawl requests, recommended the use of Simsimi official API, Faster ~

It is hoped that this article will help you to develop PHP-based public platform.

http://www.bkjia.com/PHPjc/984616.html www.bkjia.com true http://www.bkjia.com/PHPjc/984616.html techarticle PHP-based public platform Development example, PHP Public platform example of this article describes the PHP-based public platform development method. Share to everyone for your reference. Specific as follows ...

  • Related Article

    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.