@php $datecheck = "2021-11-22"; try { // $datecheck = date('Y-m-d'); $data = array('opedate' => $datecheck); $url = "http://192.168.199.9/absWS/api/Appointment/Surgery"; $client = new \GuzzleHttp\Client(); $response = $client->post($url, [ 'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json'], 'body' => json_encode($data) ]); $json = jsonDecode($response->getBody()); if(isset($json->MessageCode)){ if($json->MessageCode==200){ havedata($json,$datecheck); } } $view['tb_hisconnect'] = DB::connection('endocapture_server') ->table('tb_hisconnect') ->where('his_status',0) ->whereDate('his_date',$datecheck) ->get(); } catch (\Exception $e) { $view['tb_hisconnect'] = DB::table('tb_hisconnect')->where('his_status',0)->get(); } $view['tb_hisconnect'] = DB::connection('endocapture_server') ->table('tb_hisconnect') ->where('his_status',0) ->whereDate('his_date',$datecheck) ->get(); $json = jsonEncode($view['tb_hisconnect']); echo $json; function insertHIS_CONNECT($j,$date){ $json = jsonEncode($j); $val['his_hn'] = $j->HN; $val['his_date'] = $date; $val['his_json'] = $json; $val['his_status'] = 0; $w['his_hn'] = $j->HN; $w['his_date'] = $date; $tb_hisconnect = DB::connection('endocapture_server')->table('tb_hisconnect')->where($w)->first(); if($tb_hisconnect==null){ DB::connection('endocapture_server')->table('tb_hisconnect')->insert($val); } } function havedata($json,$date){ foreach($json->Result as $j){ checkHN($j); insertHIS_CONNECT($j,$date); } } function checkHN($j){ $val['hn'] = $j->HN; $exname = explode(" ",$j->PTNAME); $val['firstname'] = current($exname); $val['lastname'] = end($exname); $year = date('Y')-$j->AGE; $val['birthdate'] = $year."-01-01"; if($j->MALE=="ชาย"){$gender = 1;}else{$gender = 2;} $val['gender'] = $gender; $patient = DB::table('patient')->where('hn',$j->HN)->first(); if($patient==null){ DB::table('patient')->insert($val); } } @endphp