当前位置: 代码迷 >> 综合 >> 微信公众号最佳实践 ( 7.4)空气质量
  详细解决方案

微信公众号最佳实践 ( 7.4)空气质量

热度:68   发布时间:2023-12-02 05:35:46.0

空气质量

PM25.in是广州BeatApp工作室开发的一个空气质量数据检测产品,他收录了全国190个城市,近千个监测点的实时数据(每小时更新)。数据源于国家环境保护部。

该接口申请地址为:
http://www.pm25.in/api_doc

这里写图片描述
该接口调用举例如下:
http://www.pm25.in/api/querys/aqi_details.json?stations=no&city=%E6%B7%B1%E5%9C%B3&token=5j1znBVAsnSf5xQyNQyq

[{"aqi": 71,"area": "深圳","co": 1.365,"co_24h": 1.156,"no2": 51,"no2_24h": 35,"o3": 34,"o3_24h": 68,"o3_8h": 35,"o3_8h_24h": 61,"pm10": 85,"pm10_24h": 69,"pm2_5": 51,"pm2_5_24h": 48,"quality": "良","so2": 7,"so2_24h": 7,"primary_pollutant": "颗粒物(PM2.5)","time_point": "2014-02-17T10:00:00Z" }]

这里写图片描述

这里写图片描述
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);include("airquality.php");$content = getAirQualityChina($keyword);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){
    if(isset($_SERVER['HTTP_BAE_ENV_APPID'])){   //BAErequire_once "BaeLog.class.php";$logger = BaeLog::getInstance();$logger ->logDebug($log_content);}else if(isset($_SERVER['HTTP_APPNAME'])){   //SAEsae_set_display_errors(false);sae_debug($log_content);sae_set_display_errors(true);}else if($_SERVER['REMOTE_ADDR'] != "127.0.0.1"){ //LOCAL$max_size = 10000;$log_filename = "log.xml";if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}file_put_contents($log_filename, date('H:i:s')." ".$log_content."\r\n", FILE_APPEND);}}
}?>

airquality.php

<?phpfunction getAirQualityChina($city) {
    //http://www.pm25.in/api/querys/aqi_details.json?stations=no&city=%E6%B7%B1%E5%9C%B3&token=5j1znBVAsnSf5xQyNQyq$url = "http://www.pm25.in/api/querys/aqi_details.json?stations=no&city=".urlencode($city)."&token=5j1znBVAsnSf5xQyNQyq";$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);$output = curl_exec($ch);curl_close($ch);$cityAir = json_decode($output, true);if (isset($cityAir['error'])){return $cityAir['error'];}else{$result = "空气质量指数(AQI):".$cityAir[0]['aqi']."\n"."空气质量等级:".$cityAir[0]['quality']."\n"."细颗粒物(PM2.5):".$cityAir[0]['pm2_5']."\n"."可吸入颗粒物(PM10):".$cityAir[0]['pm10']."\n"."一氧化碳(CO):".$cityAir[0]['co']."\n"."二氧化氮(NO2):".$cityAir[0]['no2']."\n"."二氧化硫(SO2):".$cityAir[0]['so2']."\n"."臭氧(O3):".$cityAir[0]['o3']."\n"."更新时间:".preg_replace("/([a-zA-Z])/i", " ", $cityAir[0]['time_point']);$aqiArray = array(); $aqiArray[] = array("Title" =>$cityAir[0]['area']."空气质量", "Description" =>$result, "PicUrl" =>"", "Url" =>"");return $aqiArray;}
}?>

api_detail.json:

[{"aqi": 71,"area": "深圳","co": 1.365,"co_24h": 1.156,"no2": 51,"no2_24h": 35,"o3": 34,"o3_24h": 68,"o3_8h": 35,"o3_8h_24h": 61,"pm10": 85,"pm10_24h": 69,"pm2_5": 51,"pm2_5_24h": 48,"quality": "良","so2": 7,"so2_24h": 7,"primary_pollutant": "颗粒物(PM2.5)","time_point": "2014-02-17T10:00:00Z" }]