Develop WeChat public platform interface parameter debugging-determine user behavior

Source: Internet
Author: User
During development, an important variable $ postObj was found, and an xml structure was obtained to load the object. because of the event sent by the user, I only knew that the user sent a message and I processed it. today, a user needs to subscribe to the function of pushing a message, but no... during development, an important variable $ postObj was found, and an xml structure was obtained to load the object. because of the event sent by the user, I only knew that the user sent a message and I processed it.

Today, I want to get a user subscription to push a message, but I don't know how to determine whether the user is a subscription operation, canceling a subscription, or sending a message. the code is as follows:

public function responseMsg() {     global $db;     //get post data, May be due to the different environments     $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];     $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);     writeover("msg.txt",$postStr."rn","a+");     。。。。。 }

You can find that $ postStr is a string that has not been encapsulated before and print it directly to know the user's behavior. the output file is as follows:

 
  gh_a64528aca3b
   
  oSgH_jveTxJSlFK_6QSiGeVaSyk
   
  
   1394705044
   
  text
   
  ha
   
  
   5990212551746www.phprm.com353512
   
 

MsgType can be used to determine the user behavior. text indicates that the user has entered a text message. if it is subscription, the event is tested by everyone.

An example of a new user following a message sent by a public account is as follows:

 
  gh_a5218aca3b
   
  oSgH_jveTxJSlFK_6QSiGeVyk
   
  
   1394706271
   
  event
   
  subscribe
   
  
   
 

In this way, we can get the specific event type.

$ Msgtype = $ postObj-> MsgType; it can determine user behavior, making subsequent operations much easier.

The code for writing the log file is attached:

Open the file in read-only mode and point the file pointer to the file header.

Open the "r +" read/write mode and point the file pointer to the file header.

Open the "w" write mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it.

Open in "w +" read/write mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it.

"A" is opened in writing mode, pointing the file pointer to the end of the file. If the file does not exist, try to create it.

Open the "a +" read/write mode and point the file pointer to the end of the file. If the file does not exist, try to create it.

The PHP instance code is as follows:

Function writeover2 ($ filename, $ data, $ method = "rb +", $ iflock = 1) {@ touch ($ filename);/* create if the file does not exist. file_exists can be used for verification and other file creation functions can be used. the test result is quite efficient */$ handle = @ fopen ($ filename, $ method); if ($ iflock) {flock ($ handle, LOCK_EX);} fwrite ($ handle, $ data); if ($ method = "rb +") ftruncate ($ handle, strlen ($ data); fclose ($ handle );}


Link to this article:

Add to favorites ^ please keep the tutorial address.

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.