zoukankan      html  css  js  c++  java
  • php篇二 之微信公众号开发

    博主提示玩微信公众号必须要有自己的服务器,其次有自己的微信公众平台,将自己的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 = "您好,欢迎您关注温馨指南"."
    "."回复数字'1',输出文本消息."."
    "."回复数字'2',输出图文消息."."
    "."回复地理位置,输出位置";
                                 $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;
            }
        }
    }
    
    ?>
    

      

  • 相关阅读:
    Android杂谈ubuntu系统下adb连接小米2
    Android UI设计ListView的页脚(footer)的使用
    Android杂谈关于Android的nodpi,xhdpi,hdpi,mdpi,ldpi
    ”该证书已被签发机构吊销“错误解决方案
    Android杂谈RelativeLayout中的baseline是什么?
    ubuntu下git更改默认编辑器
    Android UI设计ListView Item的OnItemLongClickListener同时监听两种事件
    Android UI设计ListView的item选中效果
    Ubuntu下ssh服务器文件操作命令
    ubuntu下emacs的配置(cedit,ecb)
  • 原文地址:https://www.cnblogs.com/skyyuan/p/8966424.html
Copyright © 2011-2022 走看看