Introduction to the development of micro-credit public platform based on PHP _php example

Source: Internet
Author: User
Tags cdata format message openid php and

This paper illustrates the development method of micro-credit public platform based on PHP. Share to everyone for your reference. Specifically as follows:

Recently in doing micro-trust public platform development, a breath wrote more than 20 functions, very interesting ~

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

In layman's parlance, the user uses the micro-letter to send the message-> the micro-letter sends the data to the developer-> the developer processing the message and returns the data to the micro-letter-> Micro-letter sends the return data to the user, during the data interaction through the XML completes, is so simple.

Here is an example to develop a micro-letter intelligent Chat Robot:

1. Registered micro-trust public platform account

Micro-trust public platform:
https://mp.weixin.qq.com/

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

2. Application Server/Virtual host

No server/Virtual host of child shoes can be used with BAE and SAE, not much introduced.

3. Open Developer Mode

The micro-credit public platform has two modes, one is the edit mode (fool mode), simple but single function. The other is the developer model, which can be developed to implement complex functions. Two modes are mutually exclusive, it is obvious that the login micro-public platform and the "Advanced Features" menu to open the developer mode.

4. Fill in the interface configuration information

Also configured in the Advanced Features menu, you need to configure two parameters:
URL: Developer application Access address, currently only supports 80 ports, to "http://www.YoonPer.com/weixin/index.php" for example.
TOKEN: Fill in at will, use to generate the signature, take "Yoonper" as an example.
Fill out the following code to save the index.php and upload to the http://www.YoonPer.com/weixin/directory, and finally click "Submit" to complete the verification.

<?php
define ("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 micro-trust public platform to verify the correct URL access, the study code has no substantive significance, the verification can be deleted files, do not elaborate, interested in children's shoes can be viewed official documents.
Micro-trust Public Platform API Documentation:
http://mp.weixin.qq.com/wiki/index.php

5. Development of micro-credit public platform function

OK, as mentioned above, the data interaction between the micro-trust platform and the developer is done through XML, and since XML is of course subject to the specification, take a look at the XML specification provided by the official interface document before embarking on the development, taking the text message as an example:

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

<xml>
<!--developer Micro-signal-->
<tousername><![ Cdata[touser]]></tousername>
<!--sender account (OpenID)-->
<fromusername><![ Cdata[fromuser]]></fromusername>
<!--message creation time (integer)-->
<createtime>12345678</ Createtime>
<!--message category (text text message)-->
<msgtype><![ Cdata1]></msgtype>
<!--message content-->
<content><![ Cdata[content]]></content>
<!--message ID (64-bit integer)-->
<msgid>1234567890123456</msgid >
</xml>

After processing the message, the developer needs to return the data to the micro-trust server:

<xml>
<!--receiver account (OpenID)-->
<tousername><![ Cdata[touser]]></tousername>
<!--developer Micro-signal-->
<fromusername><![ Cdata[fromuser]]></fromusername>
<!--message creation time (integer)-->
<createtime>12345678</ Createtime>
<!--message category (text text message)-->
<msgtype><![ Cdata1]></msgtype>
<!--reply message content-->
<content><![ Cdata[content]]></content>
<!--star operation (the message that the star has just received when the bit 0x0001 is marked)-->
<funcflag>0</ Funcflag>
</xml>

In addition to text messages, the micro-trust public platform also supports users to send picture messages, geographical information, link messages, event push, and developers can also reply to the micro-trust platform music messages and messages, all kinds of message XML specifications can also be seen in the official documents.

To look at an official PHP sample, I did some streamlining:

<?php/*-------------------------------------------------|   index.php [micro-credit public platform interface] +--------------------------------------------------|
Author:limyoonper +------------------------------------------------* * $wechatObj = new WeChat ();
$WECHATOBJ->responsemsg (); Class WeChat {public Function responsemsg () {//----------receive data----------//$postStr = $GLOBALS ["Http_raw_post_ DATA "]; Get post Data//SimpleXML parsing post xml data $POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', libxml_nocdata)
  ; $fromUsername = $POSTOBJ->fromusername; Get the Sender account (OpenID) $toUsername = $POSTOBJ->tousername; Get the receiver account $keyword = Trim ($postObj->content); Gets the message content $time = time (); Get current timestamp//----------return Data----------////return message template $TEXTTPL = "<xml> <tousername><! [cdata[%s]]></tousername> <fromusername><! [cdata[%s]]></fromusername> <CreateTime>%s</CreateTime> <msgtype><! [Cdata[%s]]></msgType> <content><!
  [cdata[%s]]></content> <FuncFlag>0</FuncFlag> </xml> "; $msgType = "text";
  Message type include (' simsimi.php '); $CONTENTSTR = Simsimi ($keyword);
  Returns the message content//format message Template $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR); Echo $resultStr; Output results}}?>

Save the code as index.php and upload it to the http://www.YoonPer.com/weixin/directory, overwriting it if the file is not deleted just now.
&NBSP
Now the user sends any message through the micro-credit public platform to return a message that the content is "http://www.YoonPer.com".
The next thing to do is to dynamically return results based on user messages ~
 
Simsimi (small yellow chicken) is the fire chat robot, I used Curl developed a free simsimi (small yellow chicken) interface, incoming keywords will return text reply, This part is not the focus of this article, not much description, directly on the code (2014.07.28 Update):

<?php/*-------------------------------------------------|   simsimi.php [Smart Chat (Simsimi)] +--------------------------------------------------| Author:limyoonper +------------------------------------------------*/function Simsimi ($keyword) {$keyword = Urlenco
  De ($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\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'm not going to answer this question ... ' }}?>

The combination of the above two pieces of code is done, it should be noted that the micro-trust server in 5 seconds to receive a response will break the connection, through this interface may be timed out, and Simsimi has been shielding the BAE and SAE crawl requests, recommended the use of Simsimi official charging API, faster ~

I hope this article is helpful to the development of the micro-trust public platform based on PHP.

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.