将http发布内容写入php中的文件

时间:2013-04-13 09:49:13

标签: php post

我将字符串xml数据发布到IIS中托管的php页面。我想要的是能够读取我在php页面中收到的字符串数据并将其写入文件。但我无法实现同样的目标。这是我的代码: -

<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){ 
echo "Hello\n";
$somecontent = print_r($_POST, TRUE);

$my_file = "resp.txt";
$handle = fopen($my_file, "w") or die('Cannot open file:  '.$my_file);
fwrite($handle, $somecontent);  
    }else {
echo "Error\n";
    }
 ?>

但是我无法创建文件或读取POST内容。如果有人想弄清楚如何解决这个问题,我会很高兴。

2 个答案:

答案 0 :(得分:0)

如果你知道输入字段名称,那么使用它。

<?php
 if($_SERVER['REQUEST_METHOD'] == "POST")
 { 
 echo "Hello\n";

 $somecontent = array();
 $somecontent[] = $_POST['firstname'];
 $somecontent[] = $_POST['lastname'];
 //etc...    

 $my_file = "resp.txt";
 $handle = fopen($my_file, "wb") or die('Cannot open file:  '.$my_file);
 fwrite($handle, $somecontent);  
 fclose($handle);
 }
 else {
 echo "Error\n";
 }
?>

答案 1 :(得分:0)

这有用吗?

if(!empty($_POST){
  file_put_contents('resp.txt', serialize($_POST));
}