php微信公众平台自定义消息回复api

花了一天写的微信公众平台自动回复,简单封装了一下。增加了记录了用户发送信息日志。错误日志


<?php
/**
 * wechatCallbackapi
 *
 * @author      83
 * @link        http://madeby83.com
 * @since       Version 1.0
 * @filesource
 */

//define your token
define("TOKEN", "weixin");

$wechatObj = new wechatCallbackapi();
if( empty($wechatObj->keyword) ) $wechatObj->responseMsg('请输入些什么');
//回复图文消息,必须是二维数组
$array = array(
            array("title"=>"testnews","des"=>"testdes","pic"=>"http://res.wx.qq.com/mpres/zh_cn/htmledition/images/logo1191e9.png","url"=>"http://mp.weixin.qq.com/cgi-bin/readtemplate?t=wxm-callbackapi-doc&lang=zh_cn"),
            array("title"=>"testnews","des"=>"testdes","pic"=>"http://res.wx.qq.com/mpres/zh_cn/htmledition/images/logo1191e9.png","url"=>"http://mp.weixin.qq.com/cgi-bin/readtemplate?t=wxm-callbackapi-doc&lang=zh_cn"),
            array("title"=>"testnews","des"=>"testdes","pic"=>"http://res.wx.qq.com/mpres/zh_cn/htmledition/images/logo1191e9.png","url"=>"http://mp.weixin.qq.com/cgi-bin/readtemplate?t=wxm-callbackapi-doc&lang=zh_cn"),
            array("title"=>"testnews","des"=>"testdes","pic"=>"http://res.wx.qq.com/mpres/zh_cn/htmledition/images/logo1191e9.png","url"=>"http://mp.weixin.qq.com/cgi-bin/readtemplate?t=wxm-callbackapi-doc&lang=zh_cn"),
            array("title"=>"testnews","des"=>"testdes","pic"=>"http://res.wx.qq.com/mpres/zh_cn/htmledition/images/logo1191e9.png","url"=>"http://mp.weixin.qq.com/cgi-bin/readtemplate?t=wxm-callbackapi-doc&lang=zh_cn"),
            );
$wechatObj->responseImgMsg($array);

//回复文字信息
// $wechatObj->responseMsg("测试");

class wechatCallbackapi
{
    var $postStr;
    var $postObj;
    var $fromUserName;
    var $toUsername;
    var $keyword;

    public function __construct()
    {
        //验证
        if(!$this->checkSignature()) $this->_error('验证失败');

        //get post data, May be due to the different environments
        $this->postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

        if( empty($this->postStr) ) $this->_error('获取数据失败', true);
        $this->postObj = simplexml_load_string($this->postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
        $this->fromUsername = $this->postObj->FromUserName;
        $this->toUsername = $this->postObj->ToUserName;
        $this->keyword = trim($this->postObj->Content);
        $this->_log( $this->keyword );
    }

    //官方给的 处女用
    public function valid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }

    //官方给的,验证签名
    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];    
                
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
        
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }

    public function responseMsg($msg, $flag = 0)
    {
        if( empty($msg) ) return;
        $textTpl = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA]></MsgType>
                    <Content><![CDATA[%s]]></Content>
                    <FuncFlag>%d</FuncFlag>
                    </xml>";
        $time = time();
        $resultStr = sprintf($textTpl, $this->fromUsername, $this->toUsername, $time, $msg, $flag);
        echo $resultStr;
    }

    //返回图文信息
    public function responseImgMsg($array, $flag = 0)
    {
        if( !is_array($array) ) return;
        $time = time();
        $textHaderTpl = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[news]]></MsgType>
                    <Content><![CDATA[]]></Content>
                    <ArticleCount>%d</ArticleCount>
                    <Articles>";
        $textContentTpl = "<item>
                    <Title><![CDATA[%s]]></Title>
                    <Description><![CDATA[%s]]></Description>
                    <PicUrl><![CDATA[%s]]></PicUrl>
                    <Url><![CDATA[%s]]></Url>
                    </item>";
        $textFooterTpl = "</Articles>
                    <FuncFlag>%d</FuncFlag>
                    </xml>";
        
        $resultHaderStr = sprintf($textHaderTpl, $this->fromUsername, $this->toUsername, $time, count($array));
        foreach ($array as $key => $value) {
            $resultContentStr .= sprintf($textContentTpl, $value['title'], $value['des'], $value['pic'], $value['url']);
        }
        $resultFooterStr = sprintf($textFooterTpl, $flag);
        echo $resultStr = $resultHaderStr,$resultContentStr,$resultFooterStr;
    }

    private function _error($e_msg = 'error')
    {
        $date = date('Y-m-d H:i:s');
        error_log("$date $e_msg\n", 3, 'e.txt');
        exit;
    }

    private function _log($msg = '')
    {
        if(!$msg) return;
        $date = date('Y-m-d H:i:s');
        error_log("$date $msg\n", 3, 'key.txt');
    }
}
?>

Comments are closed.