使用 PHP 将 Post 数据写入文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4742898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write Post data to file with PHP
提问by Mark Ismail
I need to post data using the code below, to php file that will save it in a text file. I just don't know how to create the php file to receive the data below and save it in a text file. as simple as possible.
我需要使用下面的代码将数据发布到将其保存在文本文件中的 php 文件。我只是不知道如何创建 php 文件来接收下面的数据并将其保存在文本文件中。尽可能简单。
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("stringData", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringData", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
String responseText = EntityUtils.toString(response.getEntity());
tv.setText(responseText);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
回答by Hamish
Simple as:
简单如:
file_put_contents('test.txt', file_get_contents('php://input'));
回答by ekillaby
1) In PHP, to get POST data from an incoming request use the $_POST array. The POST array in PHP is associative, which means that each incoming parameter will be a key-value pair. In development it's helpful to understand what you're actually getting in $_POST. You can dump the contents using printf() or var_dump() like the following.
1) 在 PHP 中,要从传入请求中获取 POST 数据,请使用 $_POST 数组。PHP 中的 POST 数组是关联的,这意味着每个传入的参数都是一个键值对。在开发过程中,了解您在 $_POST 中实际获得的内容会很有帮助。您可以使用 printf() 或 var_dump() 转储内容,如下所示。
var_dump($_POST);
-- or --
- 或者 -
printf($_POST);
2) Choose a useful string-based format for storing the data. PHP has a serialize() function, which you could use to turn the array into a string. It's also easy to turn the array into a JSON string. I suggest using JSON since it's natural to use this notation across various languages (whereas using a PHP serialization would somewhat bind you to using PHP in the future). In PHP 5.2.0 and above the json_encode() function is built-in.
2) 选择一种有用的基于字符串的格式来存储数据。PHP 有一个 serialize() 函数,您可以使用它来将数组转换为字符串。将数组转换为 JSON 字符串也很容易。我建议使用 JSON,因为在各种语言中使用这种表示法是很自然的(而使用 PHP 序列化会使您在某种程度上绑定到将来使用 PHP)。在 PHP 5.2.0 及以上版本中, json_encode() 函数是内置的。
$json_string = json_encode($_POST);
// For info re: JSON in PHP:
// http://php.net/manual/en/function.json-encode.php
3) Store the string in a file. Try using fopen(), fwrite(), and fclose() to write the json string to a file.
3) 将字符串存储在文件中。尝试使用 fopen()、fwrite() 和 fclose() 将 json 字符串写入文件。
$json_string = json_encode($_POST);
$file_handle = fopen('my_filename.json', 'w');
fwrite($file_handle, $json_string);
fclose($file_handle);
// For info re: writing files in PHP:
// http://php.net/manual/en/function.fwrite.php
You'll want to come up with a specific location and methodology to the file paths and file names used.
您需要为所使用的文件路径和文件名提供特定的位置和方法。
Note: There's also the possibility of getting the HTTP request's POST body directly using $HTTP_RAW_POST_DATA. The raw data will be URL-encoded and it will be a string that you can write to a file as described above.
注意:还有可能使用 $HTTP_RAW_POST_DATA 直接获取 HTTP 请求的 POST 正文。原始数据将进行 URL 编码,并且是一个字符串,您可以如上所述将其写入文件。