【发布时间】:2012-04-26 21:29:48
【问题描述】:
有没有办法可以将此函数转换为从 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 作业来调用该脚本。
-
我对 PHP 和其他东西有点陌生,这将如何完成?
-
@unlucky4ever - 第一次让脚本在命令行上运行并产生所需的效果。第二,查找
crontab的手册页。 -
您在这里似乎有两个完全不同的问题。 “我如何在 PHP 中处理 SQL 数据库?”和“如何设置 cron 作业?” ... 找到介绍性教程然后就 SO 提问可以更好地回答这两个问题。