微信公众平台开发

参考:

http://mp.weixin.qq.com/wiki/index.php?title=%E9%A6%96%E9%A1%B5  开放平台

http://www.cnblogs.com/mchina/tag/%E5%BE%AE%E4%BF%A1%E5%85%AC%E4%BC%97/  微信公众平台开发系统

http://sae.sina.com.cn/?m=devcenter&catId=213   新浪服务器

http://www.cnblogs.com/txw1958/category/407134.html  方倍工作室

http://www.5icool.org/a/201306/a1908.html   

http://my.csdn.net/gdljw/code/detail/61517



<?php
/**
  * wechat php test
  */

//define your token
define("TOKEN", "sheng1990");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();
//$wechatObj->valid();

class 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);
                $RX_TYPE = trim($postObj->MsgType);

                switch($RX_TYPE)
                {
                    case "text":
                        $resultStr = $this->handleText($postObj);
                        break;
                    case "event":
                        $resultStr = $this->handleEvent($postObj);
                        break;
                    default:
                        $resultStr = "Unknow msg type: ".$RX_TYPE;
                        break;
                }
                echo $resultStr;
        }else {
            echo "";
            exit;
        }
    }

    public function handleText($postObj)
    {
        $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> ";
        //加载音乐模板
         $musicTpl= "<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>
 					</xml>";


        if(!empty( $keyword ))
        {
            //            $msgType = "text";

            switch($keyword)
            {
                case "m":
                	 $contentStr = "更多内容,敬请期待...\n";
                     break;
                case "dpw":
                    $title = "***";
                    $data  = date('Y-m-d');
                    $desription = "***希望大家积极参与!";
                    $image = "http://m############OJTxIicGu5p8yMhKhhB9fzSa119cegahic38Qwg/0";
                    $turl = "http:#############how?__b2&devicetype=android-16&version=25000104&lang=zh_CN";
                    break;
                       
            	case "pic":
                    $title = "#####";
                    $data  = date('Y-m-d');
                    $desription = "##能足不出户轻轻松松办理业务";
                    $image = "http://###gw1e8plhd17n2j20f0078dhv.jpg";
                    $turl = "http://#######com/gdtelkf?type=0";
                    break;
                case "music":
                    $title = "音乐频道";
                    $desription = "吻别 演唱:张学友\n"."作词:何启弘 作曲:殷文琦\n" ;
                    $MusicUrl = "http://music.baidu.com/song/265898";
                    $HQMusicUrl = "http://music.baidu.com/song/265898"; 
                    $msgType = "music";
                	$resultStr = sprintf($musicTpl, $fromUsername, $toUsername, $time, $msgType,$title,$desription,$MusicUrl,$HQMusicUrl);
            		echo $resultStr;
                    break;
                
                
                
                
                default :
                    $contentStr = "请重新输入正确的指令\n";
					break;

            }
            if($keyword == "pic" || $keyword == "dpw")
            {
             	$msgType = "news";
                $resultStr = sprintf($picTpl, $fromUsername, $toUsername, $time, $msgType, $title,$desription,$image,$turl);
            	echo $resultStr;
            }else
            {
            	$msgType = "text";
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
            	echo $resultStr;
            }
                   
        }else
        {
            echo "Input something...";
        }
    }

    public function handleEvent($object)
    {
        $contentStr = "";
        switch ($object->Event)
        {
            case "subscribe":
               $contentStr = "更多内容,敬请期待...\n";
                break;
            default :
                $contentStr = "Unknow Event: ".$object->Event;
                break;
        }
        $resultStr = $this->responseText($object, $contentStr);
        return $resultStr;
    }

    public function responseText($object, $content, $flag=0)
    {
        $textTpl = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[text]]></MsgType>
                    <Content><![CDATA[%s]]></Content>
                    <FuncFlag>%d</FuncFlag>
                    </xml>";
        $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag);
        return $resultStr;
    }


    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;
        }
    }
}

?>



你可能感兴趣的:(微信公众平台开发)