Home > Backend Development > PHP Tutorial > PHP WeChat public account verification token, reply content, push message method

PHP WeChat public account verification token, reply content, push message method

*文
Release: 2023-03-18 20:00:02
Original
3427 people have browsed it

This article mainly introduces in detail the practical WeChat classes that implement PHP code to verify tokens, reply images\texts, and push messages. Interested friends can refer to it, I hope it will be helpful to everyone.

The specific code is as follows

<?php
class Wechat{
  private $data = array();
  public function __construct($token){
    $this -> auth($token, $wxuser) || exit;
    if(IS_GET){
      echo($_GET[&#39;echostr&#39;]);
      exit;
    }else{
      $xml = file_get_contents("php://input");
  
      $xml = new SimpleXMLElement($xml);
  //file_put_contents(&#39;/var/log/test.txt&#39;, $xml,FILE_APPEND);
      $xml || exit;
      foreach ($xml as $key => $value){
        $this -> data[$key] = strval($value);
      }
    }
  }
  public function request(){
    return $this -> data;
  }
  public function response($content, $type = &#39;text&#39;, $flag = 0){
    $this -> data = array(&#39;ToUserName&#39; => $this -> data[&#39;FromUserName&#39;], &#39;FromUserName&#39; => $this -> data[&#39;ToUserName&#39;], &#39;CreateTime&#39; => NOW_TIME, &#39;MsgType&#39; => $type);
    $this -> $type($content);
    $this -> data[&#39;FuncFlag&#39;] = $flag;
    $xml = new SimpleXMLElement(&#39;<xml></xml>&#39;);
    $this -> data2xml($xml, $this -> data);
    exit($xml -> asXML());
  }
  private function text($content){
    $this -> data[&#39;Content&#39;] = $content;
  }
  private function music($music){
    list($music[&#39;Title&#39;], $music[&#39;Description&#39;], $music[&#39;MusicUrl&#39;], $music[&#39;HQMusicUrl&#39;]) = $music;
    $this -> data[&#39;Music&#39;] = $music;
  }
  private function news($news){
    $articles = array();
    foreach ($news as $key => $value){
      list($articles[$key][&#39;Title&#39;], $articles[$key][&#39;Description&#39;], $articles[$key][&#39;PicUrl&#39;], $articles[$key][&#39;Url&#39;]) = $value;
      if($key >= 9){
        break;
      }
    }
    $this -> data[&#39;ArticleCount&#39;] = count($articles);
    $this -> data[&#39;Articles&#39;] = $articles;
  }
  private function transfer_customer_service($content){
    $this -> data[&#39;Content&#39;] = &#39;&#39;;
  }
  private function data2xml($xml, $data, $item = &#39;item&#39;){
    foreach ($data as $key => $value){
      is_numeric($key) && $key = $item;
      if(is_array($value) || is_object($value)){
        $child = $xml -> addChild($key);
        $this -> data2xml($child, $value, $item);
      }else{
        if(is_numeric($value)){
          $child = $xml -> addChild($key, $value);
        }else{
          $child = $xml -> addChild($key);
          $node = dom_import_simplexml($child);
          $node -> appendChild($node -> ownerDocument -> createCDATASection($value));
        }
      }
    }
  }
  private function auth($token){
    $signature = $_GET["signature"];
    $timestamp = $_GET["timestamp"];
    $nonce = $_GET["nonce"];
    $tmpArr = array($token, $timestamp, $nonce);
    sort($tmpArr, SORT_STRING);
    $tmpStr = implode($tmpArr);
    $tmpStr = sha1($tmpStr);
    if(trim($tmpStr) == trim($signature)){
      return true;
    }else{
      return false;
    }
    return true;
  }
}
?>
Copy after login

Related recommendations:

##Detailed explanation of TP access to WeChat public account payment

Summary of common error messages in the development and configuration of WeChat public accounts

WeChat public accounts implement user management functions

The above is the detailed content of PHP WeChat public account verification token, reply content, push message method. 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