[WeChat] Querying real-time environmental indexes from WeChat official account

在中国,微信拥有庞大的用户群。微信公众号功能的接入,成为了信息发布者和用户群之间十分便捷的互动方式。开发者在创建公众号并获得接口权限后,可定制开发自定义功能。

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;
      }
    }
  }
?>

Leave a Reply

Your email address will not be published. Required fields are marked *