微信内置浏览器的 User Agent
如何判断微信内置浏览器,首先需要获取微信内置浏览器的User Agent,经过在 iPhone 上微信的浏览器的检测,它的 User Agent 是:
Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_3 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Mobile/10B329 MicroMessenger/5.0.1
所以通过识别 MicroMessenger 这个关键字来确定是否微信内置的浏览器了。
js判断
function is_weixin(){ var ua = navigator.userAgent.toLowerCase(); if(ua.match(/MicroMessenger/i)=="micromessenger") { return true; } else { return false; } }
PHP判断
<?php $agent = $_SERVER[‘HTTP_USER_AGENT‘] if( strtopos( $agent, ‘MicroMessenger‘) { echo ‘微信浏览器‘; }else if( strpos($userAgent,"iPhone") || strpos($userAgent,"iPad") || strpos($userAgent,"iPod") || strpos($userAgent,"iOS") ) { echo ‘苹果设备‘; } else if( strpos($userAgent,"Android") ) { echo ‘android 设备‘; } ?>
当在手机端判断到微信浏览器之后,可以使用微信的分享js脚本对其进行处理,大家可以参考微信的官方文档,这里提供一个案例,了解下大概流程和思路
<script type="text/javascript" src="/jslib/wx_share.js"></script> <script> //分享链接时给其重写分享的标题、缩略图、链接、简介等 var imgUrl = ‘<?php echo base_url(‘/images/per.png‘); ?>‘; var lineLink = ‘http://www.baidu.com/‘; var shareTitle = ‘我来给你送钱了‘; var descContent = ‘折射率,你知道是啥?‘; var timeline_title = ‘timeline_title‘; var appid = ‘‘; </script> <script> function onBridgeReady() { WeixinJSBridge.call(‘showOptionMenu‘); } </script> <script> if (typeof WeixinJSBridge == "undefined") { if (document.addEventListener) { document.addEventListener(‘WeixinJSBridgeReady‘, onBridgeReady, false); } else if (document.attachEvent) { document.attachEvent(‘WeixinJSBridgeReady‘, onBridgeReady); document.attachEvent(‘onWeixinJSBridgeReady‘, onBridgeReady); } } else { onBridgeReady(); } </script> </head>