PHP version of the micro-letter Custom reply function sample _php tips

Source: Internet
Author: User
Tags cdata current time php define

This article describes the PHP version of the micro-letter custom reply function. Share to everyone for your reference, specific as follows:

After configuring the server, you can use PHP to achieve automatic reply.

Code in the index.php

 <?php define ("TOKEN", "Weixin"); $wechatObj = new Wechatcallbackapitest (); if (Isset ($ _get[' Echostr ']) {$WECHATOBJ->valid ();} else{$wechatObj->responsemsg (), class Wechatcallbackapitest {public function valid () {$echoStr = $_get["
    Echostr "];
      if ($this->checksignature ()) {header (' content-type:text ');
      Echo $echoStr;
    Exit
    The Private Function checksignature () {$signature = $_get["signature"];
    $timestamp = $_get["timestamp"];
    $nonce = $_get["nonce"];
    $token = token;
    $TMPARR = Array ($token, $timestamp, $nonce);
    Sort ($TMPARR, sort_string);
    $TMPSTR = implode ($TMPARR);
    $TMPSTR = SHA1 ($TMPSTR);
    if ($tmpStr = = $signature) {return true;
    }else{return false;
    The Public Function responsemsg () {$postStr = $GLOBALS ["Http_raw_post_data"]; if (!empty ($POSTSTR)) {$POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', LIBXml_nocdata);
      Get Data $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 ($keyword = = "?" | | $keyword = = "?
        ")//Get user information {$msgType =" text "; $CONTENTSTR = Date ("Y-m-d h:i:s", Time ());
        The content of the reply $resultStr = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
      Echo $resultStr;
      }}else{echo "";
    Exit }}?> 

Effect:

When the user enters? Or, you'll get the current time.

For more information on PHP related content readers can view the site topics: "PHP micro-credit Development Skills summary", "PHP coding and transcoding Operation skills Summary", "PHP Network Programming Skills Summary", "PHP basic Grammar Introductory Course", "PHP string (String) Usage Summary", "php+ MySQL Database operations Introduction tutorial and PHP Common database operation Skills Summary

I hope this article will help you with the PHP program design.

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.