[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.


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.


For now, the available queries are S716 and S818.

开发过程 tutorial

1 注册公众号 (register an official account)

微信公众号注册地址:(Register URL:)


2 获取开发者权限 (apply for developer permission)

开发 -> 基本配置

3 服务器响应文件 (server config)



  $wechatObj = new wechatCallbackapiTest();

  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;");
      $PM25=$row[0]; $PM25_level=$row[1];
      $TEM=$row[2]; $TEM_level=$row[3];
      $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>
          if ($keyword=="蒙楼") {
            $contentStr = '此刻蒙楼室内PM2.5浓度为 '.$PM25.' μg/m3('.$PM25_level.
            $msgType = "text";
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
            echo $resultStr;
        }else{ echo ''; }
      } else {
        echo '';

7 thoughts on “[WeChat] Querying real-time environmental indexes from WeChat official account

  1. 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!

  2. 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!

  3. 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!

  4. 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!

  5. 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.

Leave a Reply

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