我很难知道如何从文本文件中获取POST数据。我的文本文件中包含以下文本:
2017年10月11日星期三,04:53 AM,reader_name =%22Impinj%20RFID%20Reader%22& mac_address =%2200%3a16%3a25%3a10%3a7E%3a85%22& line_ending =%0a& field_delim =%图2c&安培; FIELD_NAMES =天线%5fport%2cepc%2cpeak%5frssi%2ctid&安培; field_values = 1%2C%22503030393839343834323200%22%2C%2d50%2C%22E280110520005756DE620231%22%0A1%2C%22000000000000001000000441%22%2C%2d53%2C% 22E28011602000728E91750917%22%0A1%2C%2288000008%22%2C%2D60%2C%22E280116020007224B7650909%22%0A1%2C%22000000000000001000000441%22%2C%2d54%2C%22E28011602000728E91750917%22%0A1%2C%22503030393839343834323200%22%2C% 2d48%2C%22E280110520005756DE620231%22%0A
POST数据如下: reader_name,mac_address,line_ending,field_delim,field_names和field_values
我创建了一个代码,该代码将从文本文件中读取数据,但失败了:
<?php
$fn = "log.txt";
$read = readfile($fn);
// Store the POST variables.
$readerName = $_POST['reader_name'];
$macAddress = $_POST['mac_address'];
$lineEnding = $_POST['line_ending'];
$fieldDelim = $_POST['field_delim'];
$fieldNames = $_POST['field_names'];
$fieldValues = $_POST['field_values'];
//Connect to the database.
$con = mysqli_connect('localhost', 'root', '153624123', 'test') or die("Unable to select database");
// Replace the field delimiter with a comma.
str_replace($fieldDelim, ",", $fieldNames);
// Break the field values up into rows.
$rows = explode("\n", $fieldValues);
// Remove the last row. It's always blank
if (sizeof($rows))
array_pop($rows);
$fieldNames = "reader_name,mac_address," . $fieldNames;
foreach ($rows as $row) {
$row = $readerName . "," . $macAddress . "," .
$row;
$query = mysqli_query($con, "INSERT INTO tags ($fieldNames) VALUES ($row)");
echo "INSERT INTO tags ($fieldNames) VALUES ($row)" . '<br />';
}
?>
我尝试在谷歌搜索其他方式,但没有找到运气。文本文件来自我们为我们的项目购买的RFID,称为&#34; speedway connect&#34;产品。它有一个网站,他们发布了HTTP POST的源代码。我跟着一切,但我没有运气。
答案 0 :(得分:0)
使用以下内容:
$read = readfile($fn);
$string =urldecode(explode("^",$read)[1]);//or urldecode(preg_split('@(?=reader_name)@',$read)[1]); demo https://ideone.com/lfEn40
$post = [];
foreach (explode('&', $string) as $chunk) {
$param = explode("=", $chunk);
$post[$param[0]] = isset($param[1])?$param[1]:'';
}
$readerName = $post['mac_address'];
//....
更改文件生成器功能中的以下行
fwrite($fp, date("l F d, Y, h:i A") . "^" . $rawPostData . "\n");
//.....................................^ change here the coma into ^
答案 1 :(得分:0)
$ _ POST 设置为: 内容类型:application / x-www-form-urlencoded 换句话说,对于标准网络表单。
是基于任何表单/帖子创建的日志文件?如果没有那么你就不会从$ _POST变量中获取数据,所以你需要从文件读取数据,而不是像@madalin那样的$ _POST变量