PHP Micro-trust public Platform Development (iii) subscription event handling _php instance

Source: Internet
Author: User
Tags cdata sprintf trim

First, Introduction

New users focus on the micro-credit public platform, will produce a subscription event, that is, the Subscribe event, the default code does not respond to this event in response to processing.

After new users focus on the public platform, you may want to know what the platform provides and how to use the platform, which is, in layman's terms, the "manual" of the platform.

This article will detail the processing of the Subscribe event, reply to the corresponding information to enhance the interaction.

Second, the analysis of ideas

Micro-letters currently provide five types of messages, respectively:

    • Text message;
    • Picture message (image);
    • Location message (location);
    • Link message (links);
    • Incident push (event);

When a message is received, the message type needs to be judged first and then processed for different types of messages. In the event push, the event type is divided into three kinds, subscribe (subscription), unsubscribe (unsubscribe), click (Custom menu click event), but also need to add a judgment, after the subscribe event, according to the set good welcome message, reply to the user.

Iii. Judging message types

$POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', libxml_nocdata);
$RX _type = Trim ($postObj->msgtype);

Switch ($RX _type)
{case
  "text":
    $resultStr = $this->handletext ($POSTOBJ);
    break;
  Case "Event":
    $resultStr = $this->handleevent ($POSTOBJ);
    break;
  Default:
    $resultStr = "Unknow msg type:". $RX _type;
    break;


Description

$RX _type = Trim ($postObj->msgtype);   
get the message type; case

"text":
$resultStr = $this->handletext ($POSTOBJ);   
Use the Handletext () function to process text messages; case

"event":
$resultStr = $this->handleevent ($POSTOBJ);
Use the Handleevent () function to handle event push;

Iv. judging the type of event

Switch ($object->event)
{case
  "subscribe":
    $contentStr = "Thank you for your concern" Zhuo Jin Suzhou "." \ n "." Micro-signal: Zhuojinsz "." \ n "." Excellent splendid, Suzhou City, we provide you with Suzhou Local life guide, Suzhou Related information inquiries, do the best Suzhou Micro-trust platform. "." \ n "." The current platform features are as follows: "." \ n "." "1" Check the weather, such as input: Suzhou weather "." \ n "." "2" check public transport, such as input: Suzhou bus 178 "." \ n "." "3" translation, such as input: translation I love You "." \ n "." "4" Suzhou information inquiries, such as input: Suzhou Guan Qian Jie "." \ n "." More content, please look forward to ... ";
    break;
  Default:
    $contentStr = "Unknow Event:". $object->event;
    break;

Description

If it is Subscribe event, set the reply content as "Thank you for your attention" Zhuo Jin Suzhou ... ";

Five, complete code

<?php/** * WeChat PHP Test * *//define Your token ("define", "token");
$WECHATOBJ = new Wechatcallbackapitest ();
$WECHATOBJ->responsemsg ();

$WECHATOBJ->valid ();

    Class Wechatcallbackapitest {/*public function valid () {$echoStr = $_get["Echostr"];
      Valid signature, option if ($this->checksignature ()) {echo $echoStr;
    Exit }*/Public Function responsemsg () {//get post data, May is due to the different $POSTSTR =

    $GLOBALS ["Http_raw_post_data"]; Extract post Data if (!empty ($POSTSTR)) {$POSTOBJ = simplexml_load_string ($postStr, ' Simplexmleleme
        NT ', libxml_nocdata);

        $RX _type = Trim ($postObj->msgtype);
            Switch ($RX _type) {case "text": $resultStr = $this->handletext ($POSTOBJ);
          Break
            Case "Event": $resultStr = $this->handleevent ($POSTOBJ);
          Break
          Default  $RESULTSTR = "Unknow msg type:". $RX _type;
        Break
    Echo $resultStr;
      }else {echo "";
    Exit
    The Public Function Handletext ($POSTOBJ) {$fromUsername = $POSTOBJ->fromusername;
    $toUsername = $POSTOBJ->tousername;
    $keyword = Trim ($postObj->content);
    $time = time (); $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> ";
      if (!empty ($keyword)) {$msgType = "text";
      $CONTENTSTR = "Welcome to WeChat world!";
      $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
    Echo $resultStr;
}else{      echo "Input something ...";
    The Public Function handleevent ($object) {$contentStr = ""; Switch ($object->event) {case "subscribe": $CONTENTSTR = "Thank you for your concern" Zhuo Jin Suzhou "." \ n "." Micro-signal: Zhuojinsz "." \ n "." Excellent splendid, Suzhou City, we provide you with Suzhou Local life guide, Suzhou Related information inquiries, do the best Suzhou Micro-trust platform. "." \ n "." The current platform features are as follows: "." \ n "." "1" Check the weather, such as input: Suzhou weather "." \ n "." "2" check public transport, such as input: Suzhou bus 178 "." \ n "." "3" translation, such as input: translation I love You "." \ n "." "4" Suzhou information inquiries, such as input: Suzhou Guan Qian Jie "." \ n "."
        More content, please look forward to ... ";
      Break
        Default: $contentStr = "Unknow Event:". $object->event;
    Break
    $RESULTSTR = $this->responsetext ($object, $CONTENTSTR);
  return $resultStr; The Public function responsetext ($object, $content, $flag =0) {$textTpl = "<xml> &LT;TOUSERNAME&G t;<! [cdata[%s]]></tousername> <fromusername><! [cdata[%s]]></fromusername> <CreateTime>%s</CreateTime> <msgtype><! [cdata[text]]></msgtype> <content> <!
    [cdata[%s]]></content> <FuncFlag>%d</FuncFlag> </xml> ";
    $RESULTSTR = sprintf ($TEXTTPL, $object->fromusername, $object->tousername, Time (), $content, $flag);
  return $resultStr;
    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;
 }}}?>

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.