Shuiwen.php 3.1 KB
<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;


/**
 * 验证接口
 */
class Shuiwen extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';
    public $api = null;
    protected $cookiedata = null;
    function getcookie()
    {
        // 创建一个cURL资源
        $curl = curl_init();
        // 设置请求的URL
        $url = "http://60.161.5.61:8087/web/login.do?mode=db&j_username=lxslj&j_password=LxslJ@2023&validateCode=nsyc";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
        $header = substr($result, 0, $header_size);
        $body = substr($result, $header_size);
        curl_close($ch);
        // 获取响应标头中的cookie内容
        preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $header, $matches);
        $cookies = array();
        foreach ($matches[1] as $cookie) {
            parse_str($cookie, $cookieArr);
            $cookies = array_merge($cookies, $cookieArr);
        }
        //返回cookie内容
        $this->cookiedata=$cookies['JSESSIONID'];
        return $cookies['JSESSIONID'];
    }

    public function getwaterlevel()
    {
        $postres = $this->send_get("http://60.161.5.61:8087/web/realdata/realdataquerydata.do?stcdlist='80235810','80235820'&type=0");
        $postres=json_decode($postres,true);
        foreach ($postres['rows'] as $k=>$v){
            $waterleveldata = [
                'water_level' => $postres['rows'][$k]['39'],
                'reservoir_id' => 40,
                'createtime' => time(),
                'reporttime' => time(),
                'flow' => $postres['rows'][$k]['27'],
                'number' => $postres['rows'][$k]['STCD']
            ];
            Db::name("reservoir_rain_water_level")->insert($waterleveldata);

            $waterleveldata = [
                'rainfall' => $postres['rows'][$k]['1A'],
                'total_rainfall' => $postres['rows'][$k]['1F'],
                'reservoir_id' => 40,
                'createtime' => time(),
                'reporttime' => time(),
                'number' => $postres['rows'][$k]['STCD']
            ];
            Db::name("reservoir_rain_rainfall")->insert($waterleveldata);

        }
        return;
    }

    function send_get($url)
    {
        $Cookie=$this->getcookie();
        $header="Cookie:JSESSIONID=".$Cookie;
        $options = array(
            "http" => array(
                "method" => "GET", //or GET
                "header" => "Cookie:JSESSIONID=".$Cookie,
                "timeout" => 15 * 60 // 超时时间(单位:s)
            )
        );
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        return $result;
    }

    public function shuiwenlogin(){
        $postres=$this->send_get("http://60.161.5.61:8087/web/login.do?mode=db&j_username=lxslj&j_password=LxslJ@2023&validateCode=nsyc");
        print_r($postres);return;
    }
}