Home Backend Development PHP Tutorial Pitfalls encountered in the automatic reply of WeChat messages in PHP WeChat development, automatic reply of WeChat drift bottle, complete collection of WeChat automatic reply, automatic reply of WeChat group

Pitfalls encountered in the automatic reply of WeChat messages in PHP WeChat development, automatic reply of WeChat drift bottle, complete collection of WeChat automatic reply, automatic reply of WeChat group

Jul 29, 2016 am 08:49 AM
WeChat development automatic response

WeChat reply principle:

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

Then the user information and message are packaged into a data packet in XML format, and then this XML The data package is submitted to the URL set by the developer through the POST method.

Question 1: Why use $GLOBALS["HTTP_RAW_POST_DATA"] to save the POST data instead of the $_POST array?

Answer:

POST can only save standard data types, and cannot parse content such as XML, SOAP or Application/Octet-steam.

And $GLOBALS["HTTP_RAW_POST_DATA"] is the same as $_POST. If PHP can recognize the POST data, you can use $GLOBALS["HTTP_RAW_POST_DATA"] to receive it.

Question 2: What are the parameters and return values ​​of simplexml_load_file()?

Answer:

Parameter meaning

string: XML string that needs to be processed.

class: used to specify a new object, usually set to "SimpleXMLElement" to generate a class of simple XML elements.

options: Specify additional Libxml parameters, usually set to the constant LIBXML_NOCDATA, which means setting CDATA as a text node.

ns: Generally omitted

is_prefix: Generally omitted

Returns an object of the SimpleXMLElement class after the function is executed.

Function: The official account only accepts text messages and makes corresponding text replies.

<span><&#63;php 
define("TOKEN","weixin"); 
$weixinObj = new Wechat(); 
$weixinObj->valid(); 
class Wechat{ 
public function valid(){ 
$echoStr = $_GET['echostr']; 
//如果是第一次接入 
if($this->checkSignature() && $echoStr ){ 
echo $echoStr; 
exit; 
}else{ 
$this->responseMsg(); 
} 
} 
//校验方法 
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; 
} 
} 
/* 普通文本消息 
<xml> 
<ToUserName><![CDATA[toUser]]></ToUserName> 
<FromUserName><![CDATA[fromUser]]></FromUserName> 
<CreateTime>1348831860</CreateTime> 
<MsgType><![CDATA[text]]></MsgType> 
<Content><![CDATA[this is a test]]></Content> 
</xml> 
*/ 
public function responseMsg(){ 
//获取微信服务器POST请求中的数据 
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; 
if( !empty($postStr) ){ 
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); 
$fromUser = $postObj->FromUserName; 
$toUser = $postObj->ToUserName; 
$keyword = trim($postObj->Content); 
$time = time(); 
$template = "<xml> 
<ToUserName><![CDATA[%s]]></ToUserName> 
<FromUserName><![CDATA[%s]]></FromUserName> 
<CreateTime>%s</CreateTime> 
<MsgType><![CDATA[%s]]></MsgType> 
<Content><![CDATA[%s]]></Content> 
</xml>"; 
if( strtolower($postObj->MsgType)!='text' ){ 
$msgType = "text"; 
$content = "我只接受文本消息"; 
}else{ 
$msgType = "text"; 
if( !empty($keyword) ){ 
$content = "您发送的消息是:".$postObj->Content; 
}else{ 
$content = "请输入关键字";//消息为空 
} 
} 
$info = sprintf($template, $fromUser, $toUser, $time, $msgType, $content); 
echo $info; 
}else{ 
echo ""; 
exit; 
} 
} 
}</span> 
Copy after login

Function: The official account only accepts picture messages and makes corresponding text replies.

<span><&#63;php 
define("TOKEN","weixin"); 
$weixinObj = new Wechat(); 
$weixinObj->valid(); 
class Wechat{ 
public function valid(){ 
$echoStr = $_GET['echostr']; 
//如果是第一次接入 
if($this->checkSignature() && $echoStr ){ 
echo $echoStr; 
exit; 
}else{ 
$this->responseMsg(); 
} 
} 
//校验方法 
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; 
} 
} 
/* 接收图片消息格式 
<xml> 
<ToUserName><![CDATA[toUser]]></ToUserName> 
<FromUserName><![CDATA[fromUser]]></FromUserName> 
<CreateTime>1348831860</CreateTime> 
<MsgType><![CDATA[image]]></MsgType> 
<PicUrl><![CDATA[this is a url]]></PicUrl> 
<MediaId><![CDATA[media_id]]></MediaId> 
<MsgId>1234567890123456</MsgId> 
</xml> 
*/ 
public function responseMsg(){ 
//获取微信服务器POST请求中的数据 
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; 
if( !empty($postStr) ){ 
$postObj = 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 = "<xml> 
<ToUserName><![CDATA[%s]]></ToUserName> 
<FromUserName><![CDATA[%s]]></FromUserName> 
<CreateTime>%s</CreateTime> 
<MsgType><![CDATA[%s]]></MsgType> 
<Content><![CDATA[%s]]></Content> 
</xml>"; 
if( strtolower($msgType)!='image' ){ 
$msgType = "text"; 
$content = "我只接受图片消息"; 
}else{ 
$msgType = "text"; 
if( !empty( $picUrl ) ){ 
$content = "图片链接为:".$picUrl."\n"; 
$content .= "媒体id:".$mediaId; 
}else{ 
$content = "请发送图片";//消息为空 
} 
} 
$info = sprintf($template, $fromUser, $toUser, $time, $msgType, $content); 
echo $info; 
}else{ 
echo ""; 
exit; 
} 
} 
}</span>
Copy after login

The above is the knowledge that the editor has shared with you about the pitfalls encountered when automatically replying to WeChat messages. I hope it will be helpful to everyone!

The above introduces the pitfalls encountered in the automatic reply of WeChat messages in PHP WeChat development, including the content of WeChat development and automatic reply. I hope it will be helpful to friends who are interested in PHP tutorials.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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.

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

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.

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

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

PHP WeChat development: How to implement message encryption and decryption PHP WeChat development: How to implement message encryption and decryption May 13, 2023 am 11:40 AM

PHP is an open source scripting language that is widely used in web development and server-side programming, especially in WeChat development. Today, more and more companies and developers are starting to use PHP for WeChat development because it has become a truly easy-to-learn and easy-to-use development language. In WeChat development, message encryption and decryption are a very important issue because they involve data security. For messages without encryption and decryption methods, hackers can easily obtain the data, posing a threat to users.

How to implement automatic email reply in PHP How to implement automatic email reply in PHP May 22, 2023 pm 08:21 PM

PHP is a popular server-side scripting language that can be used to implement a variety of different types of applications, including automated email replies. Email autoresponder is a very useful feature that can be used to automatically reply to a series of emails, saving time and effort. In this article, I will introduce how to use PHP to implement automatic email replies. Step 1: Install PHP and web server. Before starting to implement automatic email reply, you must first install PHP and web server. For most people, Apache is the most common

Tutorial: Using PHP to develop Exchange mailbox automatic reply function Tutorial: Using PHP to develop Exchange mailbox automatic reply function Sep 11, 2023 pm 03:27 PM

Tutorial: Use PHP to develop the automatic reply function of Exchange mailbox. In modern society, email is the most commonly used method of communication between people. At work, we often receive a large number of emails, and replying to these emails may take a lot of time and energy. In order to improve work efficiency, many people hope to have an automatic reply function that can automatically reply to emails based on specific rules. This tutorial will introduce how to use PHP to develop the automatic reply function of Exchange mailbox. 1. Environment preparation Before starting development,

See all articles