update api auto sync data

This commit is contained in:
dongpd 2021-01-21 09:52:42 +07:00
parent 151b86ee6b
commit 631948f314

View File

@ -63,7 +63,7 @@ class ApiController extends Controller {
} }
public function actionTest() { public function actionTest() {
Yii::info("Hello"); Yii::info("Hello");
// echo base64_encode(file_get_contents("/var/www/html/BiFace_Server_Lite/imgs/test.jpg")); // echo base64_encode(file_get_contents("/var/www/html/BiFace_Server_Lite/imgs/test.jpg"));
// \app\models\WaitingReq::deleteAll(); // \app\models\WaitingReq::deleteAll();
// $test1 = \app\models\WaitingReq::find()->all(); // $test1 = \app\models\WaitingReq::find()->all();
@ -78,4 +78,51 @@ class ApiController extends Controller {
return; 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;
}
} }