微信公众平台 回复图文

<?php
/**
    微信公众平台
 
    开发者模式
 
    默认用户输入任何文字,均返回同一个图文信息,链接地址为手机站;
 
    可以根据变量$keyword,即用户输入的信息,进行判断,从而返回相应的信息;
 
*/
 
 
define("TOKEN", "echoder");//与管理平台的TOKEN设置一致
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();
 
class wechatCallbackapiTest
{
    public function valid()//验证接口用,管理平台后台设置的时候请调用此方法进行验证
    {
        $echoStr = $_GET["echostr"];
         
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }
 
    public function responseMsg()//接受用户信息并返回图文信息
    {
 
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
 
        if (!empty($postStr)){
                 
                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                             
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->Content);
                $time = time();
                $textTpl = "<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                            <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <Content><![CDATA[%s]]></Content>
                            <FuncFlag>0</FuncFlag>
                            </xml>";
                //加载图文模版
                $picTpl = "<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                            <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <ArticleCount>1</ArticleCount>
                            <Articles>
                            <item>
                            <Title><![CDATA[%s]]></Title>
                            <Description><![CDATA[%s]]></Description>
                            <PicUrl><![CDATA[%s]]></PicUrl>
                            <Url><![CDATA[%s]]></Url>
                            </item>
                            </Articles>
                            <FuncFlag>1</FuncFlag>
                            </xml> ";
                if(trim($postObj->MsgType) == "event" and trim($postObj->Event) == "subscribe")//判断是否是新关注
                {
                    $msgType = "text";
                    $contentStr = "您好,欢迎您关注我们,免费热线:400-000-000";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                    echo $resultStr;
                }elseif(!empty($keyword ))//用户输入的内容
                {
                    $msgType = "news";
                    $title = "XXXX公司";
                    $data  = date('Y-m-d');
                    $desription = "文字介绍内容";
                    $image = "http://图片网址/img/flash_18.jpg";
                    $turl = "http://www.114ppt.cn/";
                    $resultStr = sprintf($picTpl, $fromUsername, $toUsername, $time, $msgType, $title,$desription,$image,$turl);
                    echo $resultStr;
                }else{
                    echo "说点什么吧!";
                }
 
        }else {
            echo "请输入任意文字!";
            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;
        }
    }
}
 
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值