$val){ $device_code = $val["DEVICE_CODE"]; unset($val["DEVICE_CODE"]); foreach($val as $k=>$v){ $post_data[] = [ 'code' => $k, "value" =>$v, "parent_device_code"=> $device_code, "device_code"=>$device_code, "batch"=> $batch ]; } } // 直接拼接转发数据 $post_action_data = json_encode([ "action"=>"set_node_data", "param"=>[ "working_subclass"=>$workingsubclass, "data"=>$post_data ] ]); //检查env的转发ip是否已经配置 if(!envs("SCADA_IP") || !envs("SCADA_IP")){ echo 'ENV文件配置缺少SetDeviceProcess需要使用的SCADA_IP和SCADA_PORT配置,请先配置!'; return ''; } $ip = envs("SCADA_IP"); $port = envs("SCADA_PORT"); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $ip.':'.$port); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_action_data ); curl_setopt($ch, CURLOPT_TIMEOUT, 200); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); $return = curl_exec($ch); if (curl_errno($ch)) { curl_close($ch); throw new Exception('数据请求写入失败!'); } curl_close($ch); $resutl = json_decode($return,true); if($resutl['errcode'] > 0 ){ throw new Exception($resutl['errmsg']); } }catch(Throwable $t){ //捕获Exception和Throwable的异常,防止命令行报错导致接口无任何返回 throw new Exception($t->getMessage()); } } }