Home  >  Article  >  Backend Development  >  How to implement simple WeChat text communication in php

How to implement simple WeChat text communication in php

PHPz
PHPzOriginal
2023-04-18 10:19:06440browse

With the popularity of WeChat, more and more applications need to use WeChat's open interface to develop some business logic. This article will introduce how to use PHP to implement a simple WeChat text communication function.

1. Preparation

  1. Register a developer account and create an application

Before entering the WeChat public platform development page, you need to have a WeChat account and A certified public account. After creating a public account, you need to go to the "Development-Basic Configuration" page to obtain the AppID and AppSecret of the public account, and at the same time set the developer mode URL and token in "Server Configuration".

  1. Install the necessary PHP libraries

Using PHP to implement WeChat communication requires the installation of PHP's curl extension library and simplexml extension library. Under Linux systems, use the command sudo apt-get install php-curl php-simplexml to install.

  1. Build a development environment

Use any code editor to build a development environment. It is recommended to use PHPStorm or Sublime Text. The URLs and Tokens used in the code need to correspond to the configuration of the WeChat official account.

2. Obtain text messages sent by WeChat users

The URL and Token must be configured in the "Server Configuration" of the WeChat official account. The URL points to the php message processing program we wrote, and the Token Is the authentication corresponding to the URL.

The acceptance and processing process of text messages is as follows:

  1. Verify that the message indeed comes from the WeChat server

When the user sends a message to the official account, in WeChat The server will send the message to the URL you filled in in advance. If the "Receive message and print XML before message encryption" option is turned on in "Server Configuration", the XML file will be printed out on the page immediately. The following code is a function used to verify whether the message comes from the WeChat server.

define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
if (!isset($_GET['echostr'])) {
$wechatObj->responseMsg();
} else {
$wechatObj->valid();
}
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
if ($this->checkSignature()) {
echo $echoStr;
exit;
}
}
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;
}
}
}

The logic of the above code is very simple. When we access this php handler, if we access the url with the echostr parameter, the valid() function will be executed. In this function, it is judged whether the value of signature is legal, and if it is legal, the value of echostr is returned. If the accessed url does not have an echostr parameter, then the responseMsg() function is executed directly.

  1. Reply to text message

Reply to text message requires configuring the URL and Token in the "Server Configuration" of the WeChat official account. The URL points to the php handler we wrote, and the Token is Authentication corresponding to the URL.

private function receiveText($postObj)
{
$content = "欢迎您关注XX公司官方微信公众号,我们会竭诚为您服务!";
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$time = time();
$textTpl = "


%s


0
";
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $content);
echo $resultStr;
}

The receiveText function in the above code can receive text messages sent by users and reply with a fixed welcome text. You can change the text content of the reply based on your business needs.

3. Summary

This article briefly introduces how to use PHP to implement the WeChat text communication function, that is, how to reply to text messages sent by users. In actual applications, you can develop and customize more complex business logic according to business needs. A complete WeChat public account business logic implementation includes menus, reply messages and events, which requires the comprehensive use of PHP, MySQL and other technologies.

The above is the detailed content of How to implement simple WeChat text communication in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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