From 3e7bd8d0dd216afaac92a94f38198d9bedb6e786 Mon Sep 17 00:00:00 2001 From: dongpd Date: Mon, 16 Jan 2023 16:09:58 +0700 Subject: [PATCH] update is_connected with dynamic server --- controllers/ApiController.php | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/controllers/ApiController.php b/controllers/ApiController.php index f35bb334..07ecf66c 100644 --- a/controllers/ApiController.php +++ b/controllers/ApiController.php @@ -63,6 +63,9 @@ class ApiController extends Controller { } public function actionTest() { + $sync = \app\models\SyncUrl::find()->one(); + var_dump(parse_url($sync->data)); + exit(); Yii::info("Hello"); // echo base64_encode(file_get_contents("/var/www/html/BiFace_Server_Lite/imgs/test.jpg")); // \app\models\WaitingReq::deleteAll(); @@ -80,15 +83,22 @@ class ApiController extends Controller { public function actionSync() { Yii::$app->response->format = "json"; - if ($this->is_connected()) { - $sync = \app\models\SyncUrl::find()->one(); - if ($sync) { + $sync = \app\models\SyncUrl::find()->one(); + if ($sync) { + $parseUrl = parse_url($sync->data); + $host = $parseUrl['host']; + $port = isset($parseUrl['port']) ? $parseUrl['port'] : 80; + if ($this->is_connected($host, $port)) { $logs = \app\models\WaitingReq::find()->all(); foreach ($logs as $key => $value) { $contentJSON = json_decode($value->content); $imgPath = $contentJSON->image; - $imgBase64 = base64_encode(file_get_contents($imgPath)); + $imgBase64 = file_exists($imgPath) ? base64_encode(file_get_contents($imgPath)) : ""; + if ($imgBase64 == "") { + $value->delete(); + continue; + } $contentJSON->image = $imgBase64; $newValue = json_encode($contentJSON); @@ -113,8 +123,8 @@ class ApiController extends Controller { return ["status" => true]; } - function is_connected($ip = "google.com") { - $connected = @fsockopen($ip, 80); + function is_connected($host, $port) { + $connected = @fsockopen($host, $port); //website, port (try 80 or 443) if ($connected) { $is_conn = true; //action when connected