PHP之微信公众号验证token、回复内容、推送消息的方法

*文
Lepaskan: 2023-03-18 20:00:02
asal
3382 orang telah melayarinya

本文主要为大家详细介绍了php代码实现验证token、回复图文\文本、推送消息的实用微信类。感兴趣的小伙伴们可以参考一下,希望对大家有所帮助。

具体代码如下

<?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;
  }
}
?>
Salin selepas log masuk

相关推荐:

TP接入微信公众号支付详解

微信公众号开发配置常见错误信息汇总

微信公众号实现用户管理功能

Atas ialah kandungan terperinci PHP之微信公众号验证token、回复内容、推送消息的方法. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!