我一直在用PHP + cURL做一个有趣的自制服务器监控项目。现在,我一直在尝试使用以下命令使用cURL向PHP file发送POST数据:
echo "temp=`sensors | grep 'Core 1' | cut -c9-21 | tr -d ' '`" | curl -s -d @- http://10.0.0.201/statusboard/temp.php问题是,它似乎并没有发布任何PHP数据:
<?php
//add your server aliases here
$servers = array(
"10.0.0.201" => "Larry",
"10.0.0.56" => "Le Mac Pro",
);
if(isset( $_POST['temp'], $_POST['df'] )){
preg_match('/\d+\.\d{2}/', $_POST['temp'],$temp);
preg_match('/\d+%/', $_POST['df'],$df);
$stats = array(
"temp" => $temp[0],
"ip" => $_SERVER["REMOTE_ADDR"]
);
save_to_stats($stats);
}else{
output_stats_table();
echo "empty";
echo "<table>";
foreach ($_POST as $key => $value) {
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
function save_to_stats($stats){
$data = json_decode( file_get_contents("temps.json"), true );
$data[ $stats['ip'] ] = $stats;
file_put_contents("stats.json", json_encode($data), LOCK_EX);
}
function output_stats_table(){
global $servers;
//display data
$data = json_decode( file_get_contents("temps.json"), true );
?>
<table id="projects">
<?php foreach($data as $server => $stats): ?>
<tr>
<td class="server-status" style="width:48px;" ><?php if (time() - (int) $stats['time'] > $timeinsecondstoalert )
{
}
else
{
} ?></td>
<td class="server-name" style="width:200px; text-transform:lowercase;"><?php echo $servers[$stats['ip']] ; ?></td>
<td class="server-load" style="width:72px;"><?php echo $servers[$stats['temp']] ; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php
};
function number_of_bars($df){
$value = (int) str_replace('%', '', $df) / 10;
return round( ($value > 8 ? 8 : $value) * .8 );
}我完全不明白问题出在哪里。如果没有生成POST数据,JSON文件也不会生成,因此没有数据。
发布于 2015-05-18 13:00:00
问题是我不知道"sensors“程序的输出,它甚至可能包含一些”可怕的字符“;-)。
对于初学者,我首先会用“简单的选项”测试curl:
echo '{"temp": "Hello, world!"}' | curl -X POST -d @- http://10.0.0.201/statusboard/temp.php请注意"-X“选项,在接收的php脚本中,我会这样做:
error_log(print_r($_POST,1));这样你至少知道参数是正确的,包括你的IP地址等。
然后,您可以使用更多花哨的输入选项进入下一步--包括通过管道传输程序的输出。
https://stackoverflow.com/questions/30295244
复制相似问题