验证token、回复图文\文本、推送消息的实用微信类php代码

5年以前  |  阅读数:486 次  |  编程语言:PHP 

本文实例为大家分享了用于验证token,回复图文、文本,向用户推送消息等功能的微信类,具体代码如下


    <?php
    class Wechat{
      private $data = array();
      public function __construct($token){
        $this -> auth($token, $wxuser) || exit;
        if(IS_GET){
          echo($_GET['echostr']);
          exit;
        }else{
          $xml = file_get_contents("php://input");

          $xml = new SimpleXMLElement($xml);
      //file_put_contents('/var/log/test.txt', $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 = 'text', $flag = 0){
        $this -> data = array('ToUserName' => $this -> data['FromUserName'], 'FromUserName' => $this -> data['ToUserName'], 'CreateTime' => NOW_TIME, 'MsgType' => $type);
        $this -> $type($content);
        $this -> data['FuncFlag'] = $flag;
        $xml = new SimpleXMLElement('<xml></xml>');
        $this -> data2xml($xml, $this -> data);
        exit($xml -> asXML());
      }
      private function text($content){
        $this -> data['Content'] = $content;
      }
      private function music($music){
        list($music['Title'], $music['Description'], $music['MusicUrl'], $music['HQMusicUrl']) = $music;
        $this -> data['Music'] = $music;
      }
      private function news($news){
        $articles = array();
        foreach ($news as $key => $value){
          list($articles[$key]['Title'], $articles[$key]['Description'], $articles[$key]['PicUrl'], $articles[$key]['Url']) = $value;
          if($key >= 9){
            break;
          }
        }
        $this -> data['ArticleCount'] = count($articles);
        $this -> data['Articles'] = $articles;
      }
      private function transfer_customer_service($content){
        $this -> data['Content'] = '';
      }
      private function data2xml($xml, $data, $item = 'item'){
        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;
      }
    }
    ?>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例