
在中国,微信拥有庞大的用户群。微信公众号功能的接入,成为了信息发布者和用户群之间十分便捷的互动方式。开发者在创建公众号并获得接口权限后,可定制开发自定义功能。
In China, WeChat has large user base. The WeChat Subscription Accounts provide a convienent way to interactive between users and publishers. After creating a subscription account and obtaining developing permission, developers can develop custom functions to meet their needs.
本实验中,利用室内传感器实时的环境监测数据,远端服务器完成数据采集与归档、微信查询响应等工作。通过微信公众号“邓鼻子上脸”(微信号:zhudeng_thu)或扫描上方二维码,微信用户可以实时查询室内的实时环境数据。
In this use case, the server collects the real-time environmental monitoring data from sensors and responses the queries from WeChat. Following the subscription account 邓鼻子上脸 (WeChat ID: zhudeng_thu) or scan the QR code above, the WeChat users can query the indoor real-time environmental indexes.
目前可查询的关键词为“S716”和“S818”。
For now, the available queries are S716 and S818.
开发过程 tutorial
1 注册公众号 (register an official account)
微信公众号注册地址:(Register URL:)
https://mp.weixin.qq.com/cgi-bin/registermidpage
2 获取开发者权限 (apply for developer permission)
开发 -> 基本配置
3 服务器响应文件 (server config)
http://zhudeng.top/…./wx.php
<?php
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();
class wechatCallbackapiTest{
public function responseMsg(){
$postStr = file_get_contents("php://input", 'r');
$con = mysql_connect("server_address", "username", "password");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("Huangjingbao720", $con);
mysql_query('set names utf8');
$result = mysql_query("select pm25, pm25_level, tem, tem_level, latestUploadTime from HJB_S818 order by gTime desc limit 1;");
$row=mysql_fetch_row($result);
$PM25=$row[0]; $PM25_level=$row[1];
$TEM=$row[2]; $TEM_level=$row[3];
date_default_timezone_set("PRC");
$GTIME=date('r', substr($row[4],0,-3));
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$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>";
if(!empty($keyword)){
if ($keyword=="蒙楼") {
$contentStr = '此刻蒙楼室内PM2.5浓度为 '.$PM25.' μg/m3('.$PM25_level.
'),室内温度为'.$TEM.'°C('.$TEM_level.
'),最近更新时间:'.$GTIME;
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
}else{ echo ''; }
} else {
echo '';
exit;
}
}
}
?>
This is a very good tip particularly to those new to the blogosphere. Brief but very precise info… Thanks for sharing this one. A must read article!
Hey! This is my first visit to your blog! We are a group of volunteers and starting a new
project in a community in the same niche. Your blog provided us useful information to work
on. You have done a wonderful job!
Greetings from Florida! I’m bored at work so I decided
to browse your site on my iphone during lunch break.
I really like the info you provide here and can’t wait to take
a look when I get home. I’m amazed at how fast your blog loaded on my mobile ..
I’m not even using WIFI, just 3G .. Anyways, superb blog!
Very good post. I’m facing some of these issues as well..
Wow that was odd. I just wrote an incredibly long comment but after I clicked
submit my comment didn’t appear. Grrrr…
well I’m not writing all that over again. Anyhow, just wanted to say
fantastic blog!
I always used to read paragraph in news papers but now as I am a
user of internet therefore from now I am using net for content, thanks to web.
Attractive component to content. I just stumbled upon your website and in accession capital
to assert that I get in fact enjoyed account your weblog posts.
Any way I’ll be subscribing on your augment or even I achievement you
get right of entry to consistently fast.