2012-04-26 39 views
-2

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

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

+1

错误...是的。您只需编写一个调用该函数的脚本,然后使用它随PDO返回的数据。然后,您设置一个cron作业来调用该脚本。 – Quentin 2012-04-26 13:25:18

+0

我对PHP和东西很陌生,怎么会这样做呢? – unlucky4ever 2012-04-26 13:26:13

+1

@unlucky4ever - 首先让脚本在命令行上运行并产生所需的效果。第二,查看“crontab”的手册页。 – 2012-04-26 13:28:48

回答

4

您可以从命令行运行任何PHP文件。所以同一个命令可以在cron中使用。无需转换任何东西。

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

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

+1

好的,谢谢,我应该多研究一下......我现在可以看到我如何使它插入数据等等,但我从来没有做过cron作业,我会如何设置一个呢? – unlucky4ever 2012-04-26 13:35:35

+1

维基百科拥有您需要的所有信息。 http://en.wikipedia.org/wiki/Cron – Martin 2012-04-26 13:39:14

+1

感谢您的帮助:) – unlucky4ever 2012-04-26 13:56:08