实用的微信类,用于验证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;
    }
}
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值