CURL封装函数携带cookies提交

<?php
@header("content-type:text/html;charset=UTF-8");
//登陆函数
function http_json_data($postUrl, $curlPost, $cookiesState) {
    if (empty($postUrl) || empty($curlPost)) {
        return false;
    }
    $cookie_file = dirname(__FILE__).'/cookie.txt';
    $ch = curl_init();
    $header = array();
    $header[] = 'content-type: application/x-www-form-urlencoded';
    $header[] = 'charset=UTF-8';
    curl_setopt($ch, CURLOPT_URL,$postUrl);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    if ($cookiesState==1){
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
    }else{
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
    }
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
function lidData($host,$sid,$lid,$chaxunTime,$cookiesState=0) {
    $postUrl=$host.'/home/stoOriginal/list';
    $curlPost = 'sid='.$sid.'&lid='.$lid.'&time='.$chaxunTime;
    $lidData = http_json_data($postUrl, $curlPost , $cookiesState=0);
    $lidData= json_to_array($lidData);
    $fanhuiArray[$lid]    = array();
    $fanhuiArray[$lid]['dclName'] = $lidData['rows']['8']['dclName'];
    $fanhuiArray[$lid][$lidData['rows']['8']['time']] = $lidData['rows']['8']['stpElecValue'];
    $fanhuiArray[$lid][$lidData['rows']['20']['time']] = $lidData['rows']['20']['stpElecValue'];
    return $fanhuiArray;
}
function json_to_array($data) {
    $data = json_decode($data,JSON_UNESCAPED_UNICODE);
    return $data;
}
function array_to_json($data) {
    $data = json_encode($data,JSON_UNESCAPED_UNICODE);
    return $data;
}
    $host = 'http://www.xxxxxxx.com:9999';
    $user = 'admin';
    $password = 'admin';
    //模拟登陆
    $postUrl = $host.'/home/system_login';
    $curlPost='KEYDATA='.$user.',qt,'.md5($password).',qt,1234';
    $login = http_json_data($postUrl, $curlPost,$cookiesState=1);
        //echo $login;
    $login = json_to_array($login);
        //var_dump($login);
        //模拟登陆结束
if ($login['result']=='success'){       //判断登录成功
    $bianhao=array(
         4001,4002,4004,4010,4011,4012,4013,4014,
         4003,4005,4006,4007,4008,4009,4032,4033,4034,
         1002,1003
    );
        //查询数据
    foreach($bianhao as $key => $lid ){
        $sid = '403';
        $chaxunTime = '2022-07-27';
        $data = lidData($host,$sid,$lid,$chaxunTime,$cookiesState=0);
        var_dump($data);
    }
}
?>

 

posted @ 2022-07-28 17:10  钢锅  阅读(309)  评论(0编辑  收藏  举报