有沒有一種方法可以將此函數轉換爲從cron作業運行,然後將返回的數據插入到數據庫中?將此轉換爲cron運行?
public function ping($host, $port=25565, $timeout=0.1) {
//Set up our socket
$beginning_time = microtime(true);
$fp = fsockopen($host, $port, $errno, $errstr, $timeout);
if (!$fp) return false;
$end_time = microtime(true);
//Send 0xFE: Server list ping
fwrite($fp, "\xFE");
//Read as much data as we can (max packet size: 241 bytes)
$d = fread($fp, 256);
//Check we've got a 0xFF Disconnect
if ($d[0] != "\xFF") return false;
//Remove the packet ident (0xFF) and the short containing the length of the string
$d = substr($d, 3);
//Decode UCS-2 string
$d = mb_convert_encoding($d, 'auto', 'UCS-2');
//Split into array
$d = explode("\xA7", $d);
//Return an associative array of values
return array(
'motd' => $d[0],
'players' => intval($d[1]),
'max_players' => intval($d[2]),
'latency' => ($end_time - $beginning_time) * 1000);
}
它返回的數據是數組中最後的數據。
錯誤...是的。您只需編寫一個調用該函數的腳本,然後使用它隨PDO返回的數據。然後,您設置一個cron作業來調用該腳本。 – Quentin 2012-04-26 13:25:18
我對PHP和東西很陌生,怎麼會這樣做呢? – unlucky4ever 2012-04-26 13:26:13
@unlucky4ever - 首先讓腳本在命令行上運行併產生所需的效果。第二,查看「crontab」的手冊頁。 – 2012-04-26 13:28:48