-3

有没有办法可以将此函数转换为从 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);
}

它返回的数据是最后数组中的数据。

4

1 回答 1

4

您可以从命令行运行任何 PHP 文件。因此,可以在 cron 中使用相同的命令。无需转换任何东西。

/usr/local/bin/php /home/test.php

路径/usr/local/bin/php应该是您的 php 二进制文件的位置。

于 2012-04-26T13:29:48.937 回答