1. 程式人生 > >php篇二 之微信公眾號開發

php篇二 之微信公眾號開發

mps timestamp brush ack article time curl lB -m

博主提示玩微信公眾號必須要有自己的服務器,其次有自己的微信公眾平臺,將自己的token保存在開發者模式中,將代碼上傳到服務器中,並且保存在開發者模式中。代碼類似如下

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

//define your token
define("TOKEN", "weixin");
$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);
                $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> ";
                    //加載經緯度
                    $localtionTpl =  "<xml>
                                   <ToUserName>< ![CDATA[%s] ]></ToUserName>
                                   <FromUserName>< ![CDATA[%s] ]></FromUserName>
                                   <CreateTime>%s</CreateTime>
                                   <MsgType>< ![CDATA[%s] ]></MsgType>
                                   <Location_X>%s</Location_X>
                                   <Location_Y>%s</Location_Y>
                                   <Scale>20</Scale>
                                   <Label>< ![CDATA[%s] ]></Label>
                                   </xml>";

                switch($RX_TYPE)
                {
                    case "text":
                        if($keyword == "2")
                        {
                                 $msgType = "news";
                                 $title = "PHP二次開發網"; //標題
                                 $data  = date(‘Y-m-d‘); //時間
                                 $desription = "“車位停放情況 “"; //簡介
                                 $image = "http://p1.so.qhmsg.com/bdr/_240_/t01f4f0880ce10600f6.jpg"; //圖片地址
                                 $turl = "http://yuan.snowboy99.com/stop_car/Home/Login/login"; //鏈接地址
                             $resultStr = sprintf($picTpl, $fromUsername, $toUsername, $time, $msgType, $title,$desription,$image,$turl);
                             echo $resultStr;
                         }elseif($keyword == "1"){
                             $msgType = "text";
                             $contentStr = "您好,歡迎您關註溫馨指南,在這裏我們將為你提供良好的停車場地。";
                             $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                             echo $resultStr;

                         }

                        break;
                    case "location"://地理位置消息
                    // if(trim($postObj->MsgType) == "loaction"){
                           $msgType = "text";
                           $content = "你發送的是位置,緯度為:".$postObj->Location_X.";經度為:".$postObj->Location_Y.";縮放級別為:".$postObj->Scale.";位置為:".$postObj->Label; 
                           $result = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $content);
                           echo $result; 
                         // }
                        
                    break;
                   

                    case "event":
                        if(trim($postObj->MsgType) == "event" and trim($postObj->Event) == "subscribe")//判斷是否是新關註
                        {
                             $msgType = "text";
                             $contentStr = "您好,歡迎您關註溫馨指南"."\n"."回復數字‘1‘,輸出文本消息."."\n"."回復數字‘2‘,輸出圖文消息."."\n"."回復地理位置,輸出位置";
                             $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                             echo $resultStr;
                         }
                        break;
                    default:
                        $resultStr = "Unknow msg type: ".$RX_TYPE;
                        break;
                }
                echo $resultStr;
        }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;
        }
    }
}

?>

  

php篇二 之微信公眾號開發