工作 · · 547 阅读

获取温湿度用折线图显示

工作
public function getws(){
        header('Content-type:text/html;charset=utf-8');
    	$rtc = $temperature = $humidity = [];
    	$endtime = date('Y-m-d H:i:s');
    	$starttime = date("Y-m-d H:i:s",strtotime("-365 day"));
    	// $sn = 72170100;
    	$sn = input('sn');
    	$url = 'http://t-open.tzonedigital.cn/ajax/iHistory.ashx?M=GetTAG04&sn='.$sn;
    	$data = [
    		'BeginTime'=>$starttime,
    		'EndTime'=>$endtime,
    		'AllowPaging'=>true,
    		'PageSize'=>100,
    		'TotalCount'=>0,
    		'PageIndex'=>1
    	];
    	// $data = '{"BeginTime":"2017-10-19 00:00:00","EndTime":"2017-10-20 00:00:00","AllowPaging":true,"PageSize":100,"TotalCount":0,"PageIndex":1}';
    	$result = $this->post_data($url,json_encode($data));
    	$resarr = json_decode($result,true);

    	$getdata = $resarr['ResultList'];

    	if(empty($getdata)){
    		echo '获取数据失败';die;
    	}

    	foreach ($getdata as $k => $v) {
    		$rtc[] = $v['RTC'];
    		$temperature[] = str_replace('℃', '', $v['Temperature']);
    		$humidity[] = str_replace('%', '', $v['Humidity']);
    	}

    	$this->assign('rtc',json_encode(array_reverse($rtc)));
    	$this->assign('temperature',json_encode(array_reverse($temperature)));
    	$this->assign('humidity',json_encode(array_reverse($humidity)));
    	
    	return $this->fetch();
    }
    public function post_data($url,$data){
    	$header = array();
        $header[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36';
        $ch =curl_init($url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        // 不输出header头信息
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        $res = curl_exec($ch);
        curl_close($ch);
        // $res = mb_convert_encoding($res, "GBK","UTF-8");//不然会乱码
        return $res;
    }