From 631948f314ea4bf4d659268a1daeeb4f1ff3ddd4 Mon Sep 17 00:00:00 2001 From: dongpd Date: Thu, 21 Jan 2021 09:52:42 +0700 Subject: [PATCH] update api auto sync data --- controllers/ApiController.php | 49 ++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/controllers/ApiController.php b/controllers/ApiController.php index 93b33193..e8ab355a 100644 --- a/controllers/ApiController.php +++ b/controllers/ApiController.php @@ -63,7 +63,7 @@ class ApiController extends Controller { } public function actionTest() { - Yii::info("Hello"); + Yii::info("Hello"); // echo base64_encode(file_get_contents("/var/www/html/BiFace_Server_Lite/imgs/test.jpg")); // \app\models\WaitingReq::deleteAll(); // $test1 = \app\models\WaitingReq::find()->all(); @@ -78,4 +78,51 @@ class ApiController extends Controller { return; } + public function actionSync() { + Yii::$app->response->format = "json"; + if ($this->is_connected()) { + $sync = \app\models\SyncUrl::find()->one(); + if ($sync) { + $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)); + $contentJSON->image = $imgBase64; + + $newValue = json_encode($contentJSON); + + $options = [ + 'http' => [ + 'header' => "Content-Type: application/json", + 'method' => "POST", + 'content' => $newValue + ] + ]; + $res = json_decode(file_get_contents($sync->data, false, stream_context_create($options)), true); + if ($res['status'] == 1000) { + $modelRes = new \app\models\ResponseReq(); + $modelRes->create(json_encode($res)); + $value->delete(); + unlink($imgPath); + } + } + } + } + return ["status" => true]; + } + + function is_connected($ip = "google.com") { + $connected = @fsockopen($ip, 80); + //website, port (try 80 or 443) + if ($connected) { + $is_conn = true; //action when connected + fclose($connected); + } else { + $is_conn = false; //action in connection failure + } + return $is_conn; + } + }