Home Backend Development PHP Tutorial Text message automatic reply php code for WeChat public account development_php example

Text message automatic reply php code for WeChat public account development_php example

Aug 17, 2016 pm 01:02 PM

The example in this article shares the PHP WeChat text message automatic reply code for your reference. The specific content is as follows

1. PHP sample code download
Download address 1: http://xiazai.php.net/201608/ yuanma/phpwx(php.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

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 be due to the different environments
 $postStr = $GLOBALS["HTTP_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 = "%s0"; 
 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 Exception('TOKEN is not defined!');
 }
 
 $signature = $_GET["signature"];
 $timestamp = $_GET["timestamp"];
 $nonce = $_GET["nonce"];
 
 $token = TOKEN;
 $tmpArr = array($token, $timestamp, $nonce);
 // use SORT_STRING rule
 sort($tmpArr, SORT_STRING);
 $tmpStr = implode( $tmpArr );
 $tmpStr = sha1( $tmpStr );
 
 if( $tmpStr == $signature ){
 return true;
 }else{
 return false;
 }
 }
}

?>
Copy after login

3. Call the reply message method
Comment out $wechatObj->valid( in the wx_sample.php file );, Add the sentence "$wechatObj->responseMsg();" below it.

valid();//接口验证
$wechatObj->responseMsg();//调用回复消息方法
class wechatCallbackapiTest
{
 public function valid()
 {
 $echoStr = $_GET["echostr"];

 //valid signature , option
 if($this->checkSignature()){
 echo $echoStr;
 exit;
 }
 }

 public function responseMsg()
 {
 //get post data, May be due to the different environments
 $postStr = $GLOBALS["HTTP_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 = "%s0"; 
 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 Exception('TOKEN is not defined!');
 }
 
 $signature = $_GET["signature"];
 $timestamp = $_GET["timestamp"];
 $nonce = $_GET["nonce"];
 
 $token = TOKEN;
 $tmpArr = array($token, $timestamp, $nonce);
 // use SORT_STRING rule
 sort($tmpArr, SORT_STRING);
 $tmpStr = implode( $tmpArr );
 $tmpStr = sha1( $tmpStr );
 
 if( $tmpStr == $signature ){
 return true;
 }else{
 return false;
 }
 }
}

?>
Copy after login

4. Keyword automatic reply and follow reply
$keyword saves text messages sent by users on WeChat.
Official developer documentation: https://mp.weixin.qq.com/wiki/home/index.html(Message management-"Receive messages-Receive event push-"1. Follow/unfollow events)

There are two differences between attention events and general text messages. One is that the MsgType value is event, and the other is that the added Event value is subscribe. Since the official document (original wx_sample.php) does not extract this parameter, we need to extract it ourselves. Add two variables $msgType and $event to the program.

valid();//接口验证
$wechatObj->responseMsg();//调用回复消息方法
class wechatCallbackapiTest
{
 public function valid()
 {
 $echoStr = $_GET["echostr"];

 //valid signature , option
 if($this->checkSignature()){
 echo $echoStr;
 exit;
 }
 }

 public function responseMsg()
 {
 //get post data, May be due to the different environments
 $postStr = $GLOBALS["HTTP_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;//消息类型
 $event = $postObj->Event;//时间类型,subscribe(订阅)、unsubscribe(取消订阅)
 $textTpl = "%s0"; 
  
 switch($msgType){
  case "event":
  if($event=="subscribe"){
  $contentStr = "Hi,欢迎关注海仙日用百货!"."\n"."回复数字'1',了解店铺地址."."\n"."回复数字'2',了解商品种类.";
  } 
  break;
  case "text":
  switch($keyword){
  case "1":
  $contentStr = "店铺地址:"."\n"."杭州市江干艮山西路233号新东升市场地下室第一排."; 
  break;
  case "2":
  $contentStr = "商品种类:"."\n"."杯子、碗、棉签、水桶、垃圾桶、洗碗巾(刷)、拖把、扫把、"
   ."衣架、粘钩、牙签、垃圾袋、保鲜袋(膜)、剪刀、水果刀、饭盒等.";
  break;
  default:
  $contentStr = "对不起,你的内容我会稍后回复";
  }
  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 Exception('TOKEN is not defined!');
 }
 
 $signature = $_GET["signature"];
 $timestamp = $_GET["timestamp"];
 $nonce = $_GET["nonce"];
 
 $token = TOKEN;
 $tmpArr = array($token, $timestamp, $nonce);
 // use SORT_STRING rule
 sort($tmpArr, SORT_STRING);
 $tmpStr = implode( $tmpArr );
 $tmpStr = sha1( $tmpStr );
 
 if( $tmpStr == $signature ){
 return true;
 }else{
 return false;
 }
 }
}


?>
Copy after login

The above is the entire content of the text message automatic reply php code for WeChat public account development_php example. I hope it will be helpful to everyone's learning. For more related content, please pay attention to the PHP Chinese website (www.php .cn)!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to set up automatic reply in Xianyu How to set up automatic reply How to set up automatic reply in Xianyu How to set up automatic reply Mar 13, 2024 am 10:50 AM

Xianyu can easily meet the needs of everyone. Everyone can buy the goods they need here, and they can also sell their idle items here, making money easily. It is very cost-effective. Friends in need can You can use it to sell all the idle items that you no longer need. You can not only free up space, but also get money. You can set the price freely and others can buy it directly. Sometimes if you are not satisfied with the price and want it, you can also We will talk to you about the price or get more detailed product information. If we don’t reply at this time, we may lose a customer, so we can set up some automatic replies, which can effectively help everyone retain customers for some time. Very useful. The editor here provides you with how to set up automatic replies.

Scrapy implements crawling and analysis of WeChat public account articles Scrapy implements crawling and analysis of WeChat public account articles Jun 22, 2023 am 09:41 AM

Scrapy implements article crawling and analysis of WeChat public accounts. WeChat is a popular social media application in recent years, and the public accounts operated in it also play a very important role. As we all know, WeChat public accounts are an ocean of information and knowledge, because each public account can publish articles, graphic messages and other information. This information can be widely used in many fields, such as media reports, academic research, etc. So, this article will introduce how to use the Scrapy framework to crawl and analyze WeChat public account articles. Scr

How to set up QQ automatic reply? QQ automatic reply setting steps How to set up QQ automatic reply? QQ automatic reply setting steps Mar 15, 2024 pm 03:13 PM

QQ is a popular social networking software that not only supports multiple communication methods such as text, voice, and video, but also has rich personalized settings. Among them, the automatic reply function is a very practical setting. It can automatically send preset reply content to your friends when you are busy or unable to reply to messages in time, thus avoiding the embarrassment caused by failure to reply in time. In this way, when you are busy or temporarily away, QQ will automatically send preset replies to your friends, allowing you to enjoy social fun and easily cope with various scenarios. How to set up QQ automatic reply? QQ automatic reply setting steps 1. Unlock the phone, open the QQ application, on the homepage message page, lightly click the [Status] icon in the upper left corner, or long press your [avatar] to enter immediately

What are the differences between WeChat official account certification and non-certification? What are the differences between WeChat official account certification and non-certification? Sep 19, 2023 pm 02:15 PM

The difference between WeChat public account authentication and non-authentication lies in the authentication logo, function permissions, push frequency, interface permissions and user trust. Detailed introduction: 1. Certification logo. Certified public accounts will obtain the official certification logo, which is the blue V logo. This logo can increase the credibility and authority of the public account and make it easier for users to identify the real official public account; 2. Function permissions. Certified public accounts have more functions and permissions than uncertified public accounts. For example, certified public accounts can apply to activate the WeChat payment function to achieve online payment and commercial operations, etc.

How to set up automatic replies in Outlook 2013 - How to set up automatic replies in Outlook 2013 How to set up automatic replies in Outlook 2013 - How to set up automatic replies in Outlook 2013 Mar 05, 2024 pm 02:01 PM

Recently, many new friends who have just used Outlook 2013 have asked me how to set up automatic replies in Outlook 2013. Below, I will bring you how to set up automatic replies in Outlook 2013. Let's take a look below. After logging in, the following interface will pop up. Click on the file on the upper left to enter. The following file options interface pops up, click [Auto Reply] to enter the automatic reply setting interface. By default, automatic sending of emails is not checked. We can click and check to set the period of time for automatic reply, as shown in the figure below. You can finally edit the content that needs to be restored. Click OK when the editing is completed, and then ask your colleagues to send you an email to see the effect.

Practical crawler combat in Python: WeChat public account crawler Practical crawler combat in Python: WeChat public account crawler Jun 10, 2023 am 09:01 AM

Python is an elegant programming language with powerful data processing and web crawling capabilities. In this digital era, the Internet is filled with a large amount of data, and crawlers have become an important means of obtaining data. Therefore, Python crawlers are widely used in data analysis and mining. In this article, we will introduce how to use Python crawler to obtain WeChat public account article information. WeChat official account is a popular social media platform for publishing articles online and is an important tool for promotion and marketing of many companies and self-media.

How to use PHP to develop automatic reply email function? How to use PHP to develop automatic reply email function? Sep 11, 2023 pm 09:51 PM

How to use PHP to develop automatic reply email function? With the widespread use of email, the function of automatically replying to emails has become an essential feature in the daily work of many organizations and individuals. Using PHP to develop automatic reply email functions can help us save time and energy and improve work efficiency. In this article, we will introduce how to use PHP to develop the function of automatically replying to emails in order to better respond to the needs of email replies. First, we need a PHP library that can send and receive emails. PHPMailer is a very popular

ChatGPT Java: How to implement automatic reply function ChatGPT Java: How to implement automatic reply function Oct 25, 2023 am 08:47 AM

ChatGPTJava: How to implement the auto-reply function, specific code examples are needed. The auto-reply function is becoming more and more important in the development of modern technology. Whether it is online customer service, chatbots or social media platforms, they all need to have the ability to automatically respond to provide immediate and efficient service. This article will introduce how to use the Java programming language to implement the automatic reply function and provide specific code examples. Before we start, we need to understand how to build a basic ChatGPT model. ChatGPT is a

See all articles