$file){
$content=file_get_contents($file['tmp_name']);
if($key=='machine'){
$machine=$this->getMachine($content);
if(!$machine){
$result['message']='获取机器码失败';
exit(json_encode($result));
}else{
if(!$machine['machineCode']){
//保存失败文件
$log_file='protected/runtime/log_'.$this->schoolId.'_machine_'.date('Y_m_j_H').'.txt';
move_uploaded_file($file["tmp_name"], $log_file);
}
$params=array_merge($params,$machine);
}
}elseif($key=='count'){
$count=$this->getCount($content);
if(!$count){
$result['message']='获取打印量复印量失败';
exit(json_encode($result));
}else{
$params=array_merge($params,$count);
}
}
}
if($params){
$url = 'equip-print/coach/upload';
$http = http($url, 'post', $this->authUsername, $params);
exit($http);
// $response = formatResponse($http);
//上传数据
// $rs=$this->pushParams($params);
// if($rs){
// $result['success']=1;
// $result['message']='success';
// }
exit(json_encode($response));
}
}else{
//$params['remark']=$ip.' 网页内容获取失败';
$result['message']='网页内容获取失败';
exit(json_encode($result));
}
}
//上传参数
function pushParams($params,$method='post'){
$url = 'equip-print/coach/upload';
$http = http($url, $method, $this->authUsername, $params);
$response = formatResponse($http);
if($response['status']){
return true;
}else{
return false;
}
}
//获取机器型号
function getMachine($content){
if(!$content){
return false;
}
$modelName='';
$machineId='';
//读取型号,机器ID
$preg_str='#
(.*?)
#isu';
preg_match_all($preg_str,$content,$status);
if(isset($status[1]) && !empty($status[1])){
foreach($status[1] as $val){
if(strpos($val,'型号名')!==false){
preg_match_all ('/(.+?)<\/td>/im', $val, $matches);
if(isset($matches[1]) && !empty($matches[1])){
if(isset($matches[1][3]) && !empty($matches[1][3])){
$modelName= $matches[1][3];
}
}
}
if(strpos($val,'机器ID')!==false){
preg_match_all ('/(.+?)<\/td>/im', $val, $matches);
if(isset($matches[1]) && !empty($matches[1])){
if(isset($matches[1][3]) && !empty($matches[1][3])){
$machineId= $matches[1][3];
}
}
}
}
}
return array(
'machineCode'=>$machineId
);
}
//获取计数
function getCount($content){
if(!$content) return false;
$total=0;
$copierCount=0;
$printerCount=0;
$preg_str='#(.*?)
#isu';
preg_match_all($preg_str,$content,$count);
if(isset($count[0]) && !empty($count[0])){
//读取总计
if(isset($count[0][1]) && !empty($count[0][1])){
preg_match_all ('/(.+?)<\/td>/im', $count[0][1], $matches);
if(isset($matches[1][3]) && !empty($matches[1][3])){
$total= $matches[1][3];
}
}
//读取复印量
if(isset($count[0][2]) && !empty($count[0][2])){
preg_match_all ('/(.+?)<\/td>/im', $count[0][2], $matches);
if(isset($matches[1][3]) && !empty($matches[1][3])){
$copierCount= $matches[1][3];
}
}
//读取打印量
if(isset($count[0][3]) && !empty($count[0][3])){
preg_match_all ('/(.+?)<\/td>/im', $count[0][3], $matches);
if(isset($matches[1][3]) && !empty($matches[1][3])){
$printerCount= $matches[1][3];
}
}
}
return array(
'print'=>$total,
'copyNum'=>$copierCount,
'printNum'=>$printerCount
);
}
//获取机器状态
function getStatus($content){
/*获取状态部分代码*/
$preg_str='#(.*?)#isu';
preg_match_all($preg_str,$content,$status);
$getStatusArr=array();
if(isset($status[1]) && !empty($status)){
$preg_err_key='#(.*?)#isu';
$preg_err_value='#(.*?)#isu';
$preg_list_key='#(.*?)#isu';
$preg_list_value='#(.*?)#isu';
foreach ($status[1] as $val){
preg_match_all($preg_err_key,$val,$keyArr);
preg_match_all($preg_err_value,$val,$valueArr);
if(isset($keyArr[1][0]) && isset($valueArr[1][0])){
$getStatusArr[]=array(
'key'=>strip_tags($keyArr[1][0]),
'value'=>$valueArr[1][0]
);
}
preg_match_all($preg_list_key,$val,$keyArr);
preg_match_all($preg_list_value,$val,$valueArr);
if(isset($keyArr[1][0]) && isset($valueArr[1][0])){
$getStatusArr[]=array(
'key'=>strip_tags($keyArr[1][0]),
'value'=>$valueArr[1][0]
);
}
}
}
}
//读取框架跳转地址链接
function getPageUrl(){
$ip=isset($_GET['ip'])?$_GET['ip']:'http://192.168.1.19';
//改为直接获取文件内容
//读取IP内容
$result=$this->http($ip);
//判断是否为跳转页面
preg_match_all("//u",$result,$res);
if(isset($res[1]) && isset($res[1][0]) && $res[1][0]){
//跳转页面
$gotoUrl=$ip.$res[1][0];
//拆分Url
$urlArr=explode('/cn/',$gotoUrl);
$location = $urlArr[0]."/cn/websys/webArch/getStatus.cgi"; //打印机状态
$location = $urlArr[0]."/cn/websys/status/configuration.cgi"; //打印机设备信息
}
return $location;
}
function http($url, $type='GET')
{
$response = array();
$type = strtoupper($type);
if($type=='GET'){
$curl = curl_init();
if($curl){
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 10,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
));
$response = curl_exec($curl);
curl_close($curl);
}
}elseif($type=="PUT"){
$curl = @curl_init();
if ($curl) {
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
// curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
// 'Content-Length: '. strlen($data),
));
$response = curl_exec($curl);
curl_close($curl);
}
}elseif ($type == 'DELETE'){
$curl = @curl_init();
if ($curl) {
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
));
$response = curl_exec($curl);
curl_close($curl);
}
}
elseif($type=="FORM"){
$curl = @curl_init();
if ($curl) {
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 10,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"Cache-Control: no-cache",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
),
));
$response = curl_exec($curl);
curl_close($curl);
}
}else{
$curl = @curl_init();
if ($curl) {
if(empty($params)){
$data = '{}';
}else{
$data = json_encode($params);
}
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: '. strlen($data),
));
$response = curl_exec($curl);
curl_close($curl);
}
}
return $response;
}
}