微信公众号最佳实践 ( 10.3)获取微信版本及手机系统

获取微信版本及手机系统

HTTP_USER_AGENT是用来检测浏览页面的访问者

  • 在什么操作系统(包括版本号)
  • 在什么浏览器(包含版本号)
  • 和用户个人偏好的代码

通过获取微信内置浏览器的User Agent,可以得到用户手机情况及微信版本信息。
微信公众号最佳实践 ( 10.3)获取微信版本及手机系统_第1张图片

由此可见,微信浏览器的关键词为MicroMessenger,其后面的数字代表当前的微信版本号,通过识别是否有iPhone以及Android字段以及MicroMessenger及其后面的数号可以获取及手机型号。

微信公众号最佳实践 ( 10.3)获取微信版本及手机系统_第2张图片
微信公众号最佳实践 ( 10.3)获取微信版本及手机系统_第3张图片
实现代码如下所示:

index.php整体代码如下:


/*
    CopyRight 2018 All Rights Reserved
*/

define("TOKEN", "weixin");

$wechatObj = new wechatCallbackapiTest();
if (!isset($_GET['echostr'])) {
    $wechatObj->responseMsg();
}else{
    $wechatObj->valid();
}

class wechatCallbackapiTest
{
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
            echo $echoStr;
            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 responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        if (!empty($postStr)){
            $this->logger("R ".$postStr);
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $RX_TYPE = trim($postObj->MsgType);

            switch ($RX_TYPE)
            {
                case "event":
                    $result = $this->receiveEvent($postObj);
                    break;
                case "text":
                    $result = $this->receiveText($postObj);
                    break;
            }
            $this->logger("T ".$result);
            echo $result;
        }else {
            echo "";
            exit;
        }
    }

    private function receiveEvent($object)
    {
        $content = "";
        switch ($object->Event)
        {
            case "subscribe":
                $content = "欢迎关注 德强1012 ";
                break;
            case "unsubscribe":
                $content = "取消关注";
                break;
        }
        $result = $this->transmitText($object, $content);
        return $result;
    }

    private function receiveText($object)
    {
        $keyword = trim($object->Content);
        switch ($keyword)
        {
            case "微信版本":
                $content[] = array("Title" =>"获取信息", 
                "Description" =>"获取微信版本及手机型号", 
                "PicUrl" =>"http://img.zcool.cn/community/016e9957f9a150a84a0e282bb0ddb6.jpg@900w_1l_2o_100sh.jpg", 
                "Url" =>"http://1.dq095.applinzi.com/micromessenger.php");
                break;
            default:
                $content = "";
                break;
        }
        if(is_array($content)){
            $result = $this->transmitNews($object, $content);
        }else{
            $result = $this->transmitText($object, $content);
        }
        return $result;
    }

    private function transmitText($object, $content)
    {
        $textTpl = "
                        
                        
                        %s
                        
                        
                    ";
        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
        return $result;
    }

    private function transmitNews($object, $arr_item)
    {
        if(!is_array($arr_item))
            return;

        $itemTpl = "    
                            <![CDATA[%s]]>
                            
                            
                            
                        
                    ";
        $item_str = "";
        foreach ($arr_item as $item)
            $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);

        $newsTpl = "
                        
                        
                        %s
                        
                        
                        %s
                        
                        $item_str
                    ";

        $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item));
        return $result;
    }

    private function logger($log_content)
    {
    }
}


?>

micromeeenger.php整体代码如下所示:

 
 $ua = $_SERVER['HTTP_USER_AGENT'];
 if(!strpos($ua, 'MicroMessenger')){
     $weixin = "不是微信浏览器";
 }else{
     $preg = "/MicroMessenger\/(.+)/";
     preg_match_all($preg, $ua, $new_cnt);
     $weixin = "".$new_cnt[1][0]."\n";
 }
 if(strpos($ua, 'Android')){
     $phone = "Android";
 }else if(strpos($ua, 'iPhone OS')){
     $phone = "iOS";
 }else{
     $phone = "其他";
 }
 ?>
 
 <HTML>
   <HEAD>
     <TITLE>德强1012TITLE>
     <META charset=utf-8>
     <META name=viewport content="width=device-width, user-scalable=no, initial-scale=1">
     <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css" />
     <script src="http://code.jquery.com/jquery-1.9.1.min.js">script>
     <script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js">script>
   HEAD>
   <BODY>
     <div data-role="page" id="page1">
       <div data-role="content">
         <UL data-role="listview" data-inset="true">
           <LI>
             <P>
               <div class="fieldcontain">
                 <label for="userid">微信版本label>
                 <input name="userid" id="userid" value="" type="text" >
               div>
               <div class="fieldcontain">
                 <label for="openid">手机系统label>
                 <input name="openid" id="openid" value="" type="text" >
               div>
             P>
           LI>
         UL>
       div>
       <div data-theme="b" data-role="footer" data-position="fixed">
         <h3>德强1012h3>
       div>
     div>
   BODY>
 HTML>

你可能感兴趣的:(微信公众号开发最佳实践)