理論上應該是可以
大概流程可能是
前端 -> 後端(執行 script) -> return result 給前端 -> 在前端顯示結果
後端可以透過 php 的
來執行 script
後端 ping google 的範例
index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<title>Document</title>
</head>
<body>
<div class = "row mt-5">
<div class = "col-md-4 m-auto">
<div class = "row mt-3">
<div class = "col-md-12">
<button type = "button" class = "btn btn-outline-primary btn-block" id = "btnPing">Ping Google</button>
</div>
</div>
<div class = "row mt-3">
<div class = "col-md-12" id = "pingResult"/>
</div>
</div>
</div>
</body>
<script>
$('#btnPing').click(() => {
$.ajax({
type: 'GET',
url: 'do_ping.php',
dataType: 'json',
success(res) {
console.log(res);
},
fail(xhr) {
console.log(xhr);
}
});
})
</script>
</html>
do_ping.php
<?php
$results = [];
exec("ping 8.8.8.8", $results);
foreach(array_keys($results) as $key) {
$results[$key] = iconv("cp950", "UTF-8", $results[$key]);
}
echo json_encode($results);
result