首页 >后端开发 >php教程 >PHP之微信公众号验证token、回复内容、推送消息的方法

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

*文
*文原创
2017-12-29 18:39:393422浏览

本文主要为大家详细介绍了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;
  }
}
?>

相关推荐:

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

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

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

以上是PHP之微信公众号验证token、回复内容、推送消息的方法的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn