PHP micro-letter development of WeChat message automatically reply to the pits encountered, PHP automatic reply _php Tutorial

Source: Internet
Author: User
Tags cdata php define sha1

PHP Development News automatically reply to the pits encountered, PHP automatic reply


Principle of reply:

When the ordinary user sends a message to the public account, the server first receives the message sent by the user;

The user information and messages are then packaged in an XML-formatted packet, and the XML packet is submitted to the developer's URL via the Post method.

question one: why use $globals["Http_raw_post_data" to save the POST data, not the $_post array?

Reply:

Post can only hold standard data types and cannot be parsed for content such as XML, SOAP, or Application/octet-steam.

and $globals["Http_raw_post_data"] and $_post is the same, if the POST data PHP can be recognized, you can use the $globals["Http_raw_post_data" to receive.

question two:simplexml_load_file () What are the parameters and return values?

Reply:

Parameter meaning

String: The XML string that needs to be processed.

Class: Used to specify a new object, usually set to "simplexmlelement", to generate a simple XML element of the classes.

Options: Specify additional libxml parameters, usually set to constant libxml_nocdata, which means that CDATA is set to a text node.

NS: General omission

Is_prefix: General Omission

Returns an object of the SimpleXMLElement class after the function execution completes.

Function: The public number only accepts text messages and makes corresponding text replies.

<?php define ("TOKEN", "Weixin"); $WEIXINOBJ = new Wechat (); $WEIXINOBJ->valid (); Class wechat{Public Function valid () {$echoStr = $_get[' echostr ');//If it is the first time access if ($this->checksignature () && $ ECHOSTR) {echo $echoStr; exit;} else{$this->responsemsg ();}} Calibration method 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;}} /* Plain Text message
  
    
     1348831860 
     toUser 
 
     
     fromUser 
 
     
     text 
 
     
     this is a test 
 
      
  
  
  
  
  

   
  */Public Function responsemsg () {//Gets the data in the server POST request $POSTSTR = $GLOBALS ["Http_raw_post_data"]; if (!empty ($POSTSTR)) {$p Ostobj = simplexml_load_string ($postStr, ' simplexmlelement ', libxml_nocdata); $fromUser = $POSTOBJ->fromusername; $toUser = $POSTOBJ->tousername; $keyword = Trim ($postObj->content); $time = time (); $template = "
  
    
     %s 
     %s 
 
     
     %s 
 
     
     %s 
 
     
     %s 
 
      
  
  
  
  
  

   
  "; if (Strtolower ($POSTOBJ->msgtype)! = ' text ') {$msgType = "text"; $content = "I only accept text messages";} else{$msgType = "Text", if (!empty ($keyword)) {$content = "The message you sent is:". $POSTOBJ->content;} else{$content = "Please enter keywords";//Message Is empty}} $info = sprintf ($template, $fromUser, $toUser, $time, $msgType, $content); Echo $info; }else{echo ""; Exit } } } 

Function: The public number only accepts the picture message, and makes the corresponding text reply.

<?php define ("TOKEN", "Weixin"); $WEIXINOBJ = new Wechat (); $WEIXINOBJ->valid (); Class wechat{Public Function valid () {$echoStr = $_get[' echostr ');//If it is the first time access if ($this->checksignature () && $ ECHOSTR) {echo $echoStr; exit;} else{$this->responsemsg ();}} Calibration method 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;}} /* Receive picture message format
  
    
     1348831860 
     toUser 
 
     
     fromUser 
 
     
     image 
 
     
     this is a url 
 
     
     media_id 
 
     
    
      1234567890123456
      
  
  
  
  
  
  
  

   
  */Public Function responsemsg () {//Gets the data in the server POST request $POSTSTR = $GLOBALS ["Http_raw_post_data"]; if (!empty ($POSTSTR)) {$p Ostobj = simplexml_load_string ($postStr, ' simplexmlelement ', libxml_nocdata); $fromUser = $POSTOBJ->fromusername; $toUser = $POSTOBJ->tousername; $time = time (); $msgType = $POSTOBJ->msgtype; $PICURL = $POSTOBJ->picurl; $mediaId = $POSTOBJ->mediaid; $template = "
  
    
     %s 
     %s 
 
     
     %s 
 
     
     %s 
 
     
     %s 
 
      
  
  
  
  
  

   
  "; if (Strtolower ($msgType)! = ' image ') {$msgType = "text"; $content = "I only accept picture messages";} else{$msgType = "Text", if (!empty ($PICURL)) {$content = "picture link:". $picUrl. " \ n "; $content. = "Media ID:" $MEDIAID;} else{$content = "please send picture";//Message Is empty}} $info = sprintf ($template, $fromUser, $toUser, $time, $msgType, $content); Echo $info; }else{echo ""; Exit } } }

The above is a small part of the information to share the automatic reply under the pit of the relevant knowledge, I hope to help you!

http://www.bkjia.com/PHPjc/1125900.html www.bkjia.com true http://www.bkjia.com/PHPjc/1125900.html techarticle PHP Development of the message automatically reply to the Pit, PHP automatically reply to the principle: when the ordinary user to the public account to send messages, the server first received ...

  • 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.