怎么把微信公众平台与think结合起来

浏览:1588 发布日期:2014/02/19 分类:求助交流 关键字: 微信公众平台
我用原生态的php代码可以实现自定义菜单,自动回复等。
把原生态的类移植到think的扩展类之后在indexAction导入import('ORG.Util.Weixin');
        
        define("ACCESS_TOKEN", 'WzgxWP9gkY7hvzZI8LItWgE62ZHMvzJKc_8iGZ8jbjR74kx9tBJ9A9IrPDhRJ9GbySL-zD-4iLmnAkyA6ULsAHLQ0qWwfGEFDIpY5E1fys9FGbUbiTjLnSDEqjluPnpr-vXpiXweq8nN-VBR_uViiA');
        $wechatObj = new WechatCallbackapiTest();  
        $wechatObj->responseMsg();  
然后在通过微信互动就没有反映了,不知道那里出问题了求大神解决

类文件 weixin.class.phpclass WechatCallbackapiTest{  
    public function valid() {  
        $echoStr = $_GET["echostr"];  
  
        //valid signature , option  
        if ($this->checkSignature()) {  
            echo $echoStr;  
            exit;  
        }  
    }  
  
    public function responseMsg() {  
        //get post data, May be due to the different environments  
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];  
  
        //extract post data  
        if (!empty ($postStr)) {  
  
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);  
            $fromUsername = $postObj->FromUserName;  
            $toUsername = $postObj->ToUserName;  
            $keyword = trim($postObj->Content);  
            $Event = trim($postObj->Event); 
            $EventKey = trim($postObj->EventKey);
            $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>";  
            //测试事件推送  
            if($Event=="subscribe"){  
                $contentStr = "欢迎访问思微创想网络工作室微信公众平台!";  
                $msgType = "text";  
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);  
                echo $resultStr;  
            }  
            if($EventKey=="V1001_TODAY_MUSIC"){  
                $contentStr = "听歌!";  
                $msgType = "text";  
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);  
                echo $resultStr;  
            }
            if($EventKey=="V1001_TODAY_SINGER"){  
                $contentStr = "歌手简介!";  
                $msgType = "text";  
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);  
                echo $resultStr;  
            }
            if($EventKey=="V1001_GOOD"){  
                $contentStr = "赞一个!";  
                $msgType = "text";  
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);  
                echo $resultStr;  
            }
            if($EventKey=="V1001_HAO"){  
                $contentStr = "好!";  
                $msgType = "text";  
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);  
                echo $resultStr;  
            }
            //test  
            if($keyword==1){//文本  
                $contentStr = $keyword . "测试发送文本!";  
                $msgType = "text";  
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);  
                echo $resultStr;  
            }else if($keyword==2){//音乐  
                $textTpl="<xml>  
                         <ToUserName><![CDATA[%s]]></ToUserName>  
                         <FromUserName><![CDATA[%s]]></FromUserName>  
                         <CreateTime>%s</CreateTime>  
                         <MsgType><![CDATA[%s]]></MsgType>  
                         <Music>  
                         <Title><![CDATA[%s]]></Title>  
                         <Description><![CDATA[%s]]></Description>  
                         <MusicUrl><![CDATA[%s]]></MusicUrl>  
                         <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>  
                         </Music>  
                         <FuncFlag>0</FuncFlag>  
                         </xml>";  
                  
                $msgType = "music";  
                $msgtitle="致青春";  
                $Description="电影《致我们终将逝去的青春》 主题曲";  
                $MusicUrl="http://music.baidu.com/song/40153340#";  
                $HQMusicUrl="http://music.baidu.com/song/40153340#";  
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $msgtitle,$Description,$MusicUrl,$HQMusicUrl);  
                echo $resultStr;  
            }else if($keyword==3){//图文  
                $textTpl="<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> ";  
                  
                $msgType = "news";  
                $title1="致青春";  
                $Description="电影《致我们终将逝去的青春》 主题曲";  
                $PicUrl="http://img10.360buyimg.com/da/g13/M05/01/02/rBEhU1G6iA8IAAAAAADmPyFSPhUAAANiwAxLNcAAOZX575.jpg";  
                $Url="http://sale.jd.com/act/qYHkylDS0LVOg.html?sid=2&cid=601&aid=3678&bid=661&unit=36617&advid=60724&guv=";  
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $title1,$Description,$PicUrl,$Url);  
                echo $resultStr;  
            }  
            //  
            if (!empty ($keyword)) {  
  
                $contentStr = $keyword . "Welcome to wechat world!";  
  
                $msgType = "text";  
  
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);  
                echo $resultStr;  
            } else {  
                echo "Input something...";  
            }  
  
        } 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;  
        }  
    } 
    
    public function createMenu($data){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".ACCESS_TOKEN);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);
        if (curl_errno($ch)) {
        return curl_error($ch);
        }
        curl_close($ch);
        return $tmpInfo;
    }
 
}
最佳答案
评论( 相关
后面还有条评论,点击查看>>