Detailed explanation of the custom reply function of WeChat in PHP version

墨辰丷
Release: 2023-03-28 12:02:02
Original
1725 people have browsed it

This article mainly introduces the custom reply function of the PHP version of WeChat, and analyzes the settings and code implementation skills of the PHP version of the WeChat custom reply function in the form of a complete example. Friends in need can refer to the following

for details As follows:

#After configuring the server, you can use php to implement automatic reply.

Code in index.php

<?php
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
if (isset($_GET[&#39;echostr&#39;])) {
  $wechatObj->valid();
}else{
  $wechatObj->responseMsg();
}
class wechatCallbackapiTest
{
  public function valid()
  {
    $echoStr = $_GET["echostr"];
    if($this->checkSignature()){
      header(&#39;content-type:text&#39;);
      echo $echoStr;
      exit;
    }
  }
  private function checkSignature()
  {
    $signature = $_GET["signature"];
    $timestamp = $_GET["timestamp"];
    $nonce = $_GET["nonce"];
    $token = TOKEN;
    $tmpArr = array($token, $timestamp, $nonce);
    sort($tmpArr, SORT_STRING);
    $tmpStr = implode( $tmpArr );
    $tmpStr = sha1( $tmpStr );
    if( $tmpStr == $signature ){
      return true;
    }else{
      return false;
    }
  }
  public function responseMsg()
  {
    $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
    if (!empty($postStr)){
      $postObj = simplexml_load_string($postStr, &#39;SimpleXMLElement&#39;, 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($keyword == "?" || $keyword == "?") //获取用户信息
      {
        $msgType = "text";
        $contentStr = date("Y-m-d H:i:s",time()); // 回复的内容
        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
        echo $resultStr;
      }
    }else{
      echo "";
      exit;
    }
  }
}
?>
Copy after login

Effect:

When the user enters? Or? You will get the current time

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHP Introduction to MySQL (database related knowledge)

PHP MySQL operations and methods for reading data

PHP How to send emails

The above is the detailed content of Detailed explanation of the custom reply function of WeChat in PHP version. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source: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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template