简体   繁体   中英

cURL not POSTing to PHP

I've been working with PHP + cURL on a fun side project of homemade server monitoring. Right now, I've been trying to use cURL to send POST data to a PHP file with this command:

echo "temp=`sensors | grep 'Core 1' | cut -c9-21 | tr -d ' '`" | curl -s -d @- http://10.0.0.201/statusboard/temp.php

The problem is, it doesn't seem to be posting any data whatsoever 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 );
}

I am totally mystified as to what the problem is. Without the POST data being made, the JSON file isn't either, therefore no data.

Problem is I do not know the output of the "sensors" program, it might even include some "horrible characters" ;-).

For starters I would first test curl with "easy options":

echo '{"temp": "Hello, world!"}' | curl -X POST -d @- http://10.0.0.201/statusboard/temp.php

please observe the "-X" option and in the receiving php script I would do:

error_log(print_r($_POST,1));

That way you at least know the parameters are correct, including your IP address etc.

Then you can go to the next step with more fancy input options - including piping the output of your program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM