有没有一种方法可以将此函数转换为从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