SMS Public Number Development text message automatically reply PHP code _php instance

Source: Internet
Author: User
Tags cdata php sample code sha1 sprintf trim

This example for you to share the PHP micro-letter Text message automatically reply code, for your reference, the specific content as follows

1.PHP Sample code Download
Download Address 1:http://xiazai.jb51.net/201608/yuanma/phpwx (jb51.net). rar
Download Address 2:https://mp.weixin.qq.com/wiki/home/index.html(Start Development-"Access Guide-" PHP sample code download)

2.wx_sample.php Initial Code

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

$WECHATOBJ->valid ();

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

 Ttp_raw_post_data "];
  Extract post Data if (!empty ($POSTSTR)) {/* Libxml_disable_entity_loader is to prevent XML eXternal entity injection,
  The best way is to check the validity of XML by yourself/Libxml_disable_entity_loader (TRUE);
 $POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', libxml_nocdata);
 $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 ...";
 }}else {echo "";
 Exit } Private Function Checksignature () {//You must define TOKEN by yourself if (!defined ("TOKEN")) {throw new Ex
 Ception (' TOKEN is not defined! ');
 } $signature = $_get["signature"];
 $timestamp = $_get["timestamp"];
 
 $nonce = $_get["nonce"];
 $token = token;
 $TMPARR = Array ($token, $timestamp, $nonce);
 Use sort_string the rule SORT ($TMPARR, sort_string);
 $TMPSTR = implode ($TMPARR);
 
 $TMPSTR = SHA1 ($TMPSTR); if ($tmpStr = = $signaturE) {return true;
 }else{return false;

 }}}?>

3. Invoke the reply-to information method
  Comment out $wechatobj->valid () in the wx_sample.php file, and add a phrase "$WECHATOBJ-> Responsemsg (); ".

<?php/** * WeChat PHP Test * *//define Your token ("define", "token");
$WECHATOBJ = new Wechatcallbackapitest (); $WECHATOBJ->valid ();//interface Validation $WECHATOBJ->responsemsg ()//Call Reply message method class Wechatcallbackapitest {public

 function valid () {$echoStr = $_get["Echostr"];
 Valid signature, option if ($this->checksignature ()) {echo $echoStr;
 Exit The Public Function responsemsg () {//get post data, May is due to the different environments $postStr = $GLOBALS ["H

 Ttp_raw_post_data "];
  Extract post Data if (!empty ($POSTSTR)) {/* Libxml_disable_entity_loader is to prevent XML eXternal entity injection,
  The best way is to check the validity of XML by yourself/Libxml_disable_entity_loader (TRUE);
 $POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', libxml_nocdata);
 $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 ...";
 }}else {echo "";
 Exit } Private Function Checksignature () {//You must define TOKEN by yourself if (!defined ("TOKEN")) {throw new Ex
 Ception (' TOKEN is not defined! ');
 } $signature = $_get["signature"];
 $timestamp = $_get["timestamp"];
 
 $nonce = $_get["nonce"];
 $token = token;
 $TMPARR = Array ($token, $timestamp, $nonce);
 Use sort_string the rule SORT ($TMPARR, sort_string);
 $TMPSTR = implode ($TMPARR); $tmpStr = SHA1 ($TMPSTR);
 if ($tmpStr = = $signature) {return true;
 }else{return false;


 }}}?>

4. Keyword automatic reply and attention reply
$keyword holds the text message from the user's micro-mail.
Official developer Documentation:https://mp.weixin.qq.com/wiki/home/index.html(Message management-Receive message-Receive event push-1. Concern/cancellation of attention events)

There are two differences between the attention event and the general text message, one is that the Msgtype value is an event, and the other is that the subscribe value is added. Since the official document (the original wx_sample.php) did not extract this parameter, we need to extract it ourselves. Add two variables $msgtype and $event to the program.

<?php/** * WeChat PHP Test * *//define Your token ("define", "token");
$WECHATOBJ = new Wechatcallbackapitest (); $WECHATOBJ->valid ();//interface Validation $WECHATOBJ->responsemsg ()//Call Reply message method class Wechatcallbackapitest {public

 function valid () {$echoStr = $_get["Echostr"];
 Valid signature, option if ($this->checksignature ()) {echo $echoStr;
 Exit The Public Function responsemsg () {//get post data, May is due to the different environments $postStr = $GLOBALS ["H

 Ttp_raw_post_data "];
  Extract post Data if (!empty ($POSTSTR)) {/* Libxml_disable_entity_loader is to prevent XML eXternal entity injection,
  The best way is to check the validity of XML by yourself/Libxml_disable_entity_loader (TRUE);
 $POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', libxml_nocdata);
 $fromUsername = $POSTOBJ->fromusername;
 $toUsername = $POSTOBJ->tousername;
 $keyword = Trim ($postObj->content);
 $time = time (); $msgType = $POSTOBJ->msgtype;//Message classType $event = $POSTOBJ->event;//Time type, subscribe (subscription), unsubscribe (unsubscribe) $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> "; Switch ($msgType) {case "event": if ($event = = "Subscribe") {$contentStr = "Hi, please pay attention to the Sea Fairy daily necessities!". \ n "." Reply Number ' 1 ' to find out shop address. "." \ n "." 
  Reply to the number ' 2 ' to understand the type of product. ";}
  Break Case "text": Switch ($keyword) {case "1": $contentStr = "store Address:". " \ n "." 
  First row of basement of new Dongsheng Market, No. No. 233 Shanxi Road, Jianggan, Hangzhou, China.
  Break Case "2": $contentStr = "type of commodity:". \ n "." Cups, bowls, cotton swabs, buckets, garbage cans, dishwashing towel (brush), mop, Broom, "."
  Hangers, sticky hooks, toothpicks, garbage bags, fresh bags (film), scissors, fruit knives, lunch boxes and so on. ";
  Break
  Default: $contentStr = "Sorry, I will reply to your content later";
 } break;
 } $msgType = "text";
 $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR); Echo $resULTSTR;
 }else {echo "";
 Exit } Private Function Checksignature () {//You must define TOKEN by yourself if (!defined ("TOKEN")) {throw new Ex
 Ception (' TOKEN is not defined! ');
 } $signature = $_get["signature"];
 $timestamp = $_get["timestamp"];
 
 $nonce = $_get["nonce"];
 $token = token;
 $TMPARR = Array ($token, $timestamp, $nonce);
 Use sort_string the rule SORT ($TMPARR, sort_string);
 $TMPSTR = implode ($TMPARR);
 
 $TMPSTR = SHA1 ($TMPSTR);
 if ($tmpStr = = $signature) {return true;
 }else{return false;
 }}}?>

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

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.