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 = "您好,欢迎您关注温馨指南"."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篇二 之微信公众号开发

以上是php篇二 之微信公众号开发的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>