首頁  >  文章  >  後端開發  >  PHP之微信公眾號驗證token、回覆內容、推播訊息的方法

PHP之微信公眾號驗證token、回覆內容、推播訊息的方法

*文
*文原創
2017-12-29 18:39:393382瀏覽

本文主要為大家詳細介紹了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