【问题标题】:cURL not POSTing to PHPcURL 不发布到 PHP
【发布时间】:2015-07-29 11:42:32
【问题描述】:

我一直在使用 PHP + cURL 进行自制服务器监控的有趣项目。现在,我一直在尝试使用 cURL 通过以下命令将 POST 数据发送到 PHP file

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 文件也不是,因此没有数据。

【问题讨论】:

  • echo "temp=sensors | grep 'Core 1' | cut -c9-21 | tr -d ' '" 的输出是什么?
  • @AkshayHegde 只是输出,即 +38.0°

标签: php html bash curl


【解决方案1】:

问题是我不知道“传感器”程序的输出,它甚至可能包含一些“可怕的字符”;-)。

对于初学者,我会先用“简单的选项”测试 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地址等。

然后,您可以使用更多花哨的输入选项进入下一步 - 包括通过管道传输程序的输出。

【讨论】:

  • 好吧,嗯,我的日志说“[temp] => 39.0\xc2\xb0\n)\n”
猜你喜欢
  • 2013-01-14
  • 2019-06-03
  • 2015-10-28
  • 1970-01-01
  • 2020-04-26
  • 2015-05-09
  • 1970-01-01
  • 2022-06-17
  • 2015-12-06
相关资源
最近更新 更多