update is_connected with dynamic server

This commit is contained in:
dongpd 2023-01-16 16:09:58 +07:00
parent b9388a9308
commit 3e7bd8d0dd

View File

@ -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