php代码实现如下:
function getCnzzStatData($curl_date, $cur_hour, $login_url, $login_pwd, $cnzz_log='cnzz_oel.log') { if(!extension_loaded('curl')) { $error_log = $login_url.':'.$curl_date.' '.$cur_hour."-无curl扩展<br/>\n"; file_put_contents($cnzz_log, $error_log."\r\n", FILE_APPEND); return false; } $base_url = 'http://rdcnzz.com'; preg_match('#rdcnzz.com/v1/login.php\?siteid=(\d+)#', $login_url, $match_arr); //$this->get('widget')->P($match_arr);exit; if(empty($match_arr)) { $error_log = $login_url.':'.$curl_date.' '.$cur_hour."-登陆地址错误<br/>\n"; file_put_contents($cnzz_log, $error_log."\r\n", FILE_APPEND); return false; } $siteid = $match_arr[1]; $login_url = $base_url .'/v1/login.php?t=login&siteid='.$siteid; $get_url = $base_url .'/v1/index.php?siteid='.$siteid.'&a=view&c=time_flux&st='.$curl_date.'&et='.$curl_date; $cookie_file = tempnam('./temp','cnz'); $post_fields = 'password='.$login_pwd; $ch = curl_init($login_url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0"); curl_setopt($ch, CURLOPT_ENCODING, "gzip"); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); $login_result = curl_exec($ch); $respone_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); //$this->get('widget')->P($respone_status);exit; if($respone_status != 302) { $error_log = $login_url.':'.$curl_date.' '.$cur_hour."-模拟登陆失败-".$respone_status."<br/>\n"; file_put_contents($cnzz_log, $error_log."\r\n", FILE_APPEND); return false; } if(preg_match('#<script>alert\(#', $login_result)) { $error_log = $login_url.':'.$curl_date.' '.$cur_hour."-js提示登录失败<br/>\n"; file_put_contents($cnzz_log, $error_log."\r\n", FILE_APPEND); return false; } $ch = curl_init($get_url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); $content = curl_exec($ch); $respone_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); unlink($cookie_file); if($respone_status != 200) { $error_log = $login_url.':'.$curl_date.' '.$cur_hour."-获取登陆后的统计数据界面失败-".$respone_status."<br/>\n"; file_put_contents($cnzz_log, $error_log."\r\n", FILE_APPEND); return false; } preg_match_all('#<td width="100" height="25" align="center">(.*)</td>\s*<td width="150" height="25" align="center" class="red12">(.*)</td>\s*<td width="150" height="25" align="center" class="red12">(.*)</td>#i',$content,$stat_html); $cnzz_stat_data = array(); if(!empty($stat_html) && !empty($stat_html[1]) && !empty($stat_html[2]) && !empty($stat_html[3])) { foreach($stat_html[1] as $k => $hour) { if($k > 0) { $cnzz_stat_data[] = array('hour'=>$hour, 'pv'=>$stat_html[2][$k], 'ip'=>$stat_html[3][$k]); } } return json_encode($cnzz_stat_data); } else { $error_log = $login_url.':'.$curl_date.' '.$cur_hour."-未匹配到数据<br/>\n"; file_put_contents($cnzz_log, $error_log."\r\n", FILE_APPEND); return false; } }