获取微信版本及手机系统
HTTP_USER_AGENT
是用来检测浏览页面的访问者
- 在什么操作系统(包括版本号)
- 在什么浏览器(包含版本号)
- 和用户个人偏好的代码
通过获取微信内置浏览器的User Agent,可以得到用户手机情况及微信版本信息。
由此可见,微信浏览器的关键词为MicroMessenger,其后面的数字代表当前的微信版本号,通过识别是否有iPhone以及Android字段以及MicroMessenger及其后面的数号可以获取及手机型号。
实现代码如下所示:
index.php整体代码如下:
<?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 = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
return $result;
}
private function transmitNews($object, $arr_item)
{
if(!is_array($arr_item))
return;
$itemTpl = " <item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
";
$item_str = "";
foreach ($arr_item as $item)
$item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
$newsTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<Content><![CDATA[]]></Content>
<ArticleCount>%s</ArticleCount>
<Articles>
$item_str</Articles>
</xml>";
$result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item));
return $result;
}
private function logger($log_content)
{
}
}
?>
micromeeenger.php整体代码如下所示:
<?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 = "其他";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>德强1012</TITLE>
<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="<?php echo $weixin;?>" type="text" >
</div>
<div class="fieldcontain">
<label for="openid">手机系统</label>
<input name="openid" id="openid" value="<?php echo $phone;?>" type="text" >
</div>
</P>
</LI>
</UL>
</div>
<div data-theme="b" data-role="footer" data-position="fixed">
<h3>德强1012</h3>
</div>
</div>
</BODY>
</HTML>