使用 PHP 将 JSON 数据写入文本文件
Write JSON data to text file with PHP
问题:
我有一个脚本以这种方式将 JSON 数据发送到 PHP 文件:
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "process-survey.php");
xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xmlhttp.send(JSON.stringify({uid, selected}));
问题是 JSON 数据未使用 PHP 函数写入文本文件 file_put_contents()
。
最小(工作)示例:
JSON 如控制台日志
{
"uid":1,
"selected":[
{
"questionsid":1,
"val":"1"
},
{
"questionsid":2,
"val":"1"
}
]
}
PHP
<?php
$uid = json_decode($_POST['uid'], true);
$answers = json_decode($_POST['selected'], true);
$file = $_SERVER['DOCUMENT_ROOT'] . '/association/data.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new id to the file
$current .= $uid . "\n";
foreach ($answers as $item) {
$current .= $item . "\n";
}
// Write the contents back to the file
file_put_contents($file, $current);
?>
权限
添加了以下 read/write:chmod 644 data.txt
期望输出:
uid: 1
questionid: 1, val: 1
questionid: 2, val: 1
file_put_contents()
将字符串写入文件。
而您正在做的是尝试将数组写入文件。所以你需要每次都调用它或者你可以在保存数据之前序列化数组
file_put_contents($file, serialize($array));
或
foreach ($answers as $item) {
$current .= $item . "\n";
file_put_contents($file, $item);
}
你的输入是 json,所以它不会被分解成 uid
、selected
部分,所以下面的代码是接受你的 json 并输出你的预期结果(将其放在 $_POST
中,我想这就是你的意思)。
<?php
$json = '{
"uid":1,
"selected":[
{
"questionsid":1,
"val":"1"
},
{
"questionsid":2,
"val":"1"
}
]
}';
$_POST = json_decode($json, true);
$uid = $_POST['uid'];
$answers = $_POST['selected'];
$current = ''; // fgc
// start building your expected output
$current .= "uid: $uid\n";
foreach ($answers as $item) {
$line = '';
foreach ($item as $key => $value) {
$line .= "$key: $value, ";
}
$current .= rtrim($line, ', ')."\n";
}
结果:
uid: 1
questionsid: 1, val: 1
questionsid: 2, val: 1
您可以制作文件的内容 JSON
(它是有目的的);
然后您只需操作文件数组:
$postarray= your $_POST JSON ;
//read and decode the file
$current = json_decode( file_get_contents($file) , true );
//SET ID FROM THE POST JSON
$current['uid']=$postarray[$uid];
//LOOP THE POST QUESTION AND PUT THEM IN THE ARRAY
foreach($postarray['selected'] as $q){
$current[ 'question' ][ $q[ 'questionsid' ] ]= $q[ 'val' ];
}
你最终会得到一个像这样的数组:
[
uid => 1,
questions [
1 => 1,
2 => 1
]
]
并以JSON格式写回:
// Write the contents back to the file
file_put_contents($file, json_encode($current,true) );
问题:
我有一个脚本以这种方式将 JSON 数据发送到 PHP 文件:
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "process-survey.php");
xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xmlhttp.send(JSON.stringify({uid, selected}));
问题是 JSON 数据未使用 PHP 函数写入文本文件 file_put_contents()
。
最小(工作)示例:
JSON 如控制台日志
{
"uid":1,
"selected":[
{
"questionsid":1,
"val":"1"
},
{
"questionsid":2,
"val":"1"
}
]
}
PHP
<?php
$uid = json_decode($_POST['uid'], true);
$answers = json_decode($_POST['selected'], true);
$file = $_SERVER['DOCUMENT_ROOT'] . '/association/data.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new id to the file
$current .= $uid . "\n";
foreach ($answers as $item) {
$current .= $item . "\n";
}
// Write the contents back to the file
file_put_contents($file, $current);
?>
权限
添加了以下 read/write:chmod 644 data.txt
期望输出:
uid: 1
questionid: 1, val: 1
questionid: 2, val: 1
file_put_contents()
将字符串写入文件。
而您正在做的是尝试将数组写入文件。所以你需要每次都调用它或者你可以在保存数据之前序列化数组
file_put_contents($file, serialize($array));
或
foreach ($answers as $item) {
$current .= $item . "\n";
file_put_contents($file, $item);
}
你的输入是 json,所以它不会被分解成 uid
、selected
部分,所以下面的代码是接受你的 json 并输出你的预期结果(将其放在 $_POST
中,我想这就是你的意思)。
<?php
$json = '{
"uid":1,
"selected":[
{
"questionsid":1,
"val":"1"
},
{
"questionsid":2,
"val":"1"
}
]
}';
$_POST = json_decode($json, true);
$uid = $_POST['uid'];
$answers = $_POST['selected'];
$current = ''; // fgc
// start building your expected output
$current .= "uid: $uid\n";
foreach ($answers as $item) {
$line = '';
foreach ($item as $key => $value) {
$line .= "$key: $value, ";
}
$current .= rtrim($line, ', ')."\n";
}
结果:
uid: 1
questionsid: 1, val: 1
questionsid: 2, val: 1
您可以制作文件的内容 JSON
(它是有目的的);
然后您只需操作文件数组:
$postarray= your $_POST JSON ;
//read and decode the file
$current = json_decode( file_get_contents($file) , true );
//SET ID FROM THE POST JSON
$current['uid']=$postarray[$uid];
//LOOP THE POST QUESTION AND PUT THEM IN THE ARRAY
foreach($postarray['selected'] as $q){
$current[ 'question' ][ $q[ 'questionsid' ] ]= $q[ 'val' ];
}
你最终会得到一个像这样的数组:
[
uid => 1,
questions [
1 => 1,
2 => 1
]
]
并以JSON格式写回:
// Write the contents back to the file
file_put_contents($file, json_encode($current,true) );